I’ve been having a lot of trouble with this problem. I have a listview that contains:
ImageView / contactName / TextView / CheckBox
The contactName in the listview is populated by reading the contacts on the phone from a SimpleCursorAdapter. All for elements show when the app runs, but the problem I’m having is connecting the checkboxes to their corresponding item in the list.
Through some research, I found that I must use a getView() to link the checkboxes with the items in the list, but through practice, I can’t seem to get it to work right. Furthermore, none of the examples I’ve tried really explained how to apply getView(). The most full example I’ve been working from is from here:
http://androidcocktail.blogspot.com/2012/04/adding-checkboxes-to-custom-listview-in.html
The twist is that this reads and populates my listview with my contacts:
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
lv.setAdapter(adapter);
} // END POPULATECONTACTLIST
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(chkboxAllVisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
} // END GETCONTACTS
How do I link each checkbox to the a corresponding contact items in my listview?
Ok i have created a test project for you try to understand code if any problem you are having then ask I will try to help you…
HERE IS MY ONCREATE FUNCTION OF ACTIVITY.
}
HERE IS MY CUSTOM ADAPTER.
main.xml file is this:
inflated_layout code is :
if you want to use string array instead of arraylist then replace
// IN YOUR CUSTOM ADAPTER CUNSTRUCTOR
and some more changes accordingly