Reputation: 2281
I am using a customised list view for a checklist. One image and some text is set with ArrayAdapter. When the user clicks an item on the list I want to first delete the curret Image of a Image view set with a background resource in the XML, and replace with another drawable.
Firstly image does not disappear and secondly the image i am trying to set it too seems to appear randomly and sometimes not at all:
The onListItemClicked Method
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// when a list item is clicked, first remove the XML set image cross to a replace with tick on image view
super.onListItemClick(l, v, position, id);
ImageView equitmentCrossed = (ImageView) this.findViewById(R.id.iv_equitmentCheckList_Cross);
equitmentCrossed.setBackgroundResource(0);
//displayImage.setBackground(null);
equitmentCrossed.setBackgroundResource(R.drawable.tick);
}//end onListItemClicked
The Array Addapter:
ArrayAdapter checklistadaptor = new ArrayAdapter<Pair<String, Integer>>(this,
R.layout.checklist_row, R.id.tv_equimentChecklistName, equitmentAndImagesArray) {
//over ride getView
public View getView(int position, View convertView, ViewGroup container) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.checklist_row, container, false);
}
TextView title = (TextView) convertView.findViewById(R.id.tv_equimentChecklistName);
Pair<String, Integer> item = getItem(position);
title.setText(item.first);
ImageView displayIcon = (ImageView) convertView.findViewById(R.id.iv_equimentChecklist);
displayIcon.setBackgroundResource(item.second);
//title.setCompoundDrawablesWithIntrinsicBounds(item.second, 0, 0, 0);
return convertView;
}
};//end getView
this.setListAdapter(checklistadaptor);
Upvotes: 0
Views: 317
Reputation: 10977
I assume you are using a ListActivity
? So when you call this.findViewById()
in onListItemClick()
, this
refers to the ListActivity
which contains multiple Views with the id iv_equitmentCheckList_Cross
(one per entry). Android will usually return the first match. Try to change it to v.findViewById(R.id.iv_equitmentCheckList_Cross)
. v
should be the View that represents the row.
Upvotes: 1