Problem with custom Listview and Checkboxes/Buttons
i´m in trouble with a custom Listview and (Checkboxes or Button). I follow a guide (the android devolopers´s cookbook) and my custom Listview show correctly. An visisble Error occured when selected Checkboxes are scrolling.(Wro开发者_Go百科ng Checkboexes are checked)
I followed the guide on http://www.vogella.de/articles/AndroidListView/article.html#listviews_performance but it doesn´t work. How to save the state correctly?
Greeting Andreas
You will have to save the checked items in a list variable (in your Adapter subclass) and set the correct state (checked/unchecked) depending upon whether the item is present in the list variable.
private List<MyItem> mCheckedItems; //In your adapter subclass
Add/remove an item from the list variable:
//The AdapterView.OnItemClickListener, is present where you set myListView.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
MyItem item = myAdapter.getItem(position);
myAdapter.updateCheckedItems(item);
}
public void updateCheckedItems(MyItem item) //In your adapter subclass
{
if(!mCheckedItems.contains(item))
{
mCheckedItems.add(item);
}
else
{
mCheckedItems.remove(item);
}
}
Set the correct state of the checkbox:
public View getView(int position , View view , ViewGroup parent) //In your adapter subclass
{
final MyItem item = getItem(position);
CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkBox);
checkBox.setChecked(mCheckedItems.contains(item));
}
精彩评论