【发布时间】:2014-08-03 20:45:08
【问题描述】:
I have a multiple choice ListView which defaults as having a white background but when selected, the background of the item changes to blue (defined here by a hex code).
mItemState = new boolean[list.length];
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListOfMajors.this,android.R.layout.simple_list_item_multiple_choice,list);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mItemState[position] = !mItemState[position];
if (mItemState[position]){
view.setBackgroundColor(Color.parseColor("#33b5e5"));
}else{
view.setBackgroundColor(Color.WHITE);
}
}
});
现在,一切似乎都运行良好。但是,如果我选择第一个元素,最后一个元素也会改变背景颜色(但是它不会被勾选)。此外,如果我选择最后一个元素,同样的事情也会发生在第一个元素上。发生这种情况的原因似乎是什么?
【问题讨论】:
标签: java android listview colors onitemclicklistener