[英]ListView Image not updating when item clicked
我正在使用自定義列表視圖作為清單。 使用ArrayAdapter設置一幅圖像和一些文本。 當用戶單擊列表上的項目時,我要首先刪除XML視圖中具有背景資源的Image視圖集的curret Image,並替換為另一個drawable。
首先圖像不會消失,其次我試圖設置的圖像似乎也隨機出現,有時甚至根本不出現:
onListItemClicked方法
@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
數組適配器:
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);
我假設您正在使用ListActivity
? 因此,當您在onListItemClick()
調用this.findViewById()
時, this
是指ListActivity
,其中包含ID為iv_equitmentCheckList_Cross
(每個條目一個)的多個視圖。 Android通常會返回第一個匹配項。 嘗試將其更改為v.findViewById(R.id.iv_equitmentCheckList_Cross)
。 v
應該是代表該行的View。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.