I have CustomAdapter extending ArrayAdapter, in which I set some views inside LinearLayout – one row, like this (adapter):
public View getView(int position, View convertView, ViewGroup parent) {
. . .
ImageView statePic = (ImageView) view.findViewById(R.id.state);
statePic.setImageResource(R.drawable.light_green);
return view;
}
This works fine. But now I want to bind a context menu on item in my list, so I have something like this (main activity – listActivity):
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case CM_START:
View view = getListAdapter().getView(info.position, null, null);
ImageView statePic = (ImageView)view.findViewById(R.id.state);
// I'm sure this view is the right one
statePic.setImageResource(R.drawable.light_grey);
view.invalidate();
view.refreshDrawableState();
return true;
case CM_STOP:
return true;
}
return super.onContextItemSelected(item);
}
What I want to achieve with this snippet is to change image in that item, but this doesn not work (view is not refreshed with the new value). How can I do that?
You should use the targetView property of the AdapterContextMenuInfo class: