0
好的,我在我的應用程序中有一個列表活動。 我試圖做到這一點,當選擇列表項目來顯示選擇時,視圖的背景應該改變。但是現在,當我選擇一個項目時,該項目會被選中,但隨機列表項目的背景會同時發生變化,但選擇仍然正確。 我在哪裏犯錯?Android列表活動
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Cursor c = (Cursor) mAdapter.getItem(position);
String a = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String b = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (names != null)
{
if(numbers.contains(b))
{
names = names.replace(a+";", "");
numbers = numbers.replace(b+";", "");
v.setBackgroundResource(R.drawable.background);
}
else{
names = names + a + ";";
numbers = numbers + b + ";";
v.setBackgroundResource(R.drawable.background_sel);
}
}
else
{
names = a+";";
numbers = b+";";
v.setBackgroundResource(R.drawable.background_sel);
}
}