I have a custom adapter. Within my getView(), I’m trying to populate “holder.contact” with a list of my contacts, but with no success. I do have this method of populating my ListView with contacts, but it disrupts the functionality of my CheckBoxes. This code is outside of my CustomAdapter class:
public 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
public 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 + " = '" +
(false ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
} // END GETCONTACTS
But I’m trying to define it within here, but with no success (Custom Adapter class):
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.contact_entry, null, false);
holder = new ViewHolder();
holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
holder.contact = (TextView) rowView.findViewById(R.id.contactEntryText);
holder.tv = (TextView) rowView.findViewById(R.id.SMSCounttext);
holder.cb = (CheckBox) rowView.findViewById(R.id.cb_contactHistoryChk);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
Is there a way to apply my populateContactList() method within the getView()?
My constructor looks like this. “elements” is already used by another textview:
public CustomAdapter(Context context, int type, ArrayList<String> elements){
super(context, type, elements);
Your question is a little ambiguous. You have the
populateContactList()which binds aCursorwithContactsdata to aSimpleCursorAdapterand you want to use this method in thegetView()method of another customAdapter(if this is the case, don’t do this)? Or you want to use your own adapter and bind to it the data with the contacts(if this is the case, then use the results from the query,cursor, with your ownAdapter)?You don’t say what type of
Adapteryour customAdapterextends. If it is aCursorbasedAdapter, things are easy and you can use thebindViewandnewViewto access the cursor data(and you pass theCursorfromgetContacts()in the constructor(to the super class)). Otherwise put the data from theCursoryou get withgetContacts()in a structure like anArrayListand use that as the data for the adapter. (I would recommend to go with the first option).Also, it’s your job to maintain the status of
CheckBoxesfrom your row layout(there are many questions here on how to do this).Edit:
Edit:
Use the elements
ArrayListand bind it to the rows: