Finding The Checked State Of Checkbox In A Custom Listview
Solution 1:
Inside the getView()
method, you have to implement a OnCheckedChangeListener
for the CheckBox.
Here is a listener code, say for example:
ChkBx.setOnCheckedChangeListener(newOnCheckedChangeListener()
{
publicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
Solution 2:
Solution 3:
Here's a simple sample:
mContactListView.setOnItemClickListener(newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long arg3) {
CheckBoxchkContact= (CheckBox) view.findViewById(R.id.listrow_contact_chkContact);
if (chkContact.isChecked()) {
...
}
});
Solution 4:
I've had this problem with my app Hasta La Vista I've create a with custom listview with checked items and I needed to get checked items this was the solution:
ListViewlv= getListView(); //ver listviewif(lv != null){
finalSparseBooleanArraycheckedItems= lv.getCheckedItemPositions(); //get checked itemsif (checkedItems == null) {
return;
}
finalintcheckedItemsCount= checkedItems.size();
for (inti=0; i < checkedItemsCount; ++i) {
// This tells us the item position we are looking atfinalintposition= checkedItems.keyAt(i);
// This tells us the item status at the above positionfinalbooleanisChecked= checkedItems.valueAt(i);
if(isChecked){
//get item from list and do something
lv.getAdapter().getItem(position);
}
}
}
Solution 5:
As ListView views are recycled, you can not rely on listening on particular instance, you will have to store "checked" state in your data model. You will also need custom list adapter, where you create and populate individual entries. Following shall be done: - in overriden getView(), either create new view ( in case no convertView was supplied ) or inflate new one - populate viewv fields from you data model - remove old onclick listener, and set new one ( can be anonymous inner class ) modifying your data model
PS: recycling views is important if your list is big
Post a Comment for "Finding The Checked State Of Checkbox In A Custom Listview"