2016-08-17 9 views
0

は、以下の私のアダプタクラス私はアダプタでgetViewメソッドを使用しようとしていますが、グリッドビューは動作しません。あなたは私がプロファイルにImageViewのをimageresourceを設定するアダプタを作成した言うことができるように

public class ImageAdapter extends BaseAdapter { 
private Context context; 
public static ProfileData[]profileData={ 
     new ProfileData(R.drawable.profileone,"profile1"), 
     new ProfileData(R.drawable.profiletwo,"profile2"), 
     new ProfileData(R.drawable.profilethree,"profile3"), 
     new ProfileData(R.drawable.profilefour,"profile4"), 
     new ProfileData(R.drawable.profilefive,"profile5"), 
}; 



public ImageAdapter(Context context){ 
    this.context=context; 

} 

@Override 
public int getCount() { 
    return profileData.length; 
} 

@Override 
public Object getItem(int position) { 
    return profileData[position]; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(int position, View view, ViewGroup viewGroup) { 
    ImageView imageView=new ImageView(context); 
    imageView.setImageResource(profileData[position].getDrawable()); 
    return imageView; 
} 
} 

です。しかし、私は断片的にitemonclickメソッドを実装していますが、私は特定の位置でイメージビューを取得しようとしていますが、機能していない、誰かが私に理由を教えてくれますか?ここ は、私がgetView()だけのリスト項目に充填するためのビュー、および何もによって使用されることを意味しているためだImageViewの

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.fragment_welcome_fourth,container,false); 
    adapter=new ImageAdapter(getContext()); 

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

      ImageView image=(ImageView) adapter.getView(position,view,null); 
      image.setColorFilter(Color.BLUE); 

     } 
    }); 
    return view; 

} 

答えて

0

を取得しようとしています私の断片です。

あなたの行う必要があることは、あなたのリスト項目の状態を保持する何らかの変数をあなたのアダプタに持っていることです。あなたがクリックされたときにリストアイテム全体を強調表示しているので、それは "選択された"タイプの状態です。

だから、例えばあなたは、アダプタでこれを持っている可能性があり:

private boolean[] selected = new boolean[profileData.length]; 

を(これは単純な方法ではなく、最良の方法である。)

また公共セッター:

public void setSelected(int position, boolean sel) { 
     selected[position] = sel; 
     notifyDataSetChanged(); 
    } 

getViewでは、このフラグを使用して色を設定します:

@Override 
    public View getView(int position, View view, ViewGroup viewGroup) { 
     ImageView imageView=new ImageView(context); 
     imageView.setImageResource(profileData[position].getDrawable()); 
     if (selected[position]) { 
      imageView.setColorFilter(Color.BLUE); 
     } else { 
      imageView.setColorFilter(null); 
     } 
     return imageView; 
    } 

そして、イベントハンドラで、あなたがモデルを更新し、notifyDataSetChanged()を呼び出す:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
      ImageAdapter adapter = (ImageAdapter) adapterView.getAdapter(); 
      adapter.setSelected(position, true); 
     } 
    }); 

のでgetViewは常にイベントハンドラが常にモデルを更新、モデルを読み込みます。イベントハンドラ内からビューを変更しようとすると、間違っていることが分かります。

+0

グレートアンサーマン!非常に有益な – Eli

関連する問題