I have a ListView with a custom Lazy Adapter attached. I pull contacts from the phone and display them in my list. I am having an issue dealing with multiple numbers. If one contact has multiple numbers with different types they appear as different contacts as shown below:
Here is the code for getting my contact list:
public LazyAdapter getContactList(){
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneType = "";
int type = pCur.getInt(pCur.getColumnIndexOrThrow(Phone.TYPE));
switch (type){
case Phone.TYPE_HOME:
phoneType = "Home";
break;
case Phone.TYPE_MOBILE:
phoneType = "Mobile";
break;
case Phone.TYPE_WORK:
phoneType = "Work";
break;
case Phone.TYPE_FAX_HOME:
phoneType = "Home Fax";
break;
case Phone.TYPE_FAX_WORK:
phoneType = "Work Fax";
break;
default:
phoneType = "Other";
break;
}
String phoneNo = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
map = new HashMap<String, String>();
map.put(KEY_CONTACT_NAME, name);
map.put(KEY_CONTACT_NUM, phoneType + ": " + phoneNo);
//map.put(KEY_CONTACT_IMAGE, getString(getPhotoUri(id)));
contactList.add(map);
}
pCur.close();
}
}
}
adapter = new LazyAdapter(getActivity(), contactList);
return adapter;
}
And here is my lazy adapter
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.contact_list, null);
TextView contactName = (TextView)vi.findViewById(R.id.contactName); // title
TextView contactNum = (TextView)vi.findViewById(R.id.contactNum); // artist name
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> contact = new HashMap<String, String>();
contact = data.get(position);
// Setting all values in listview
contactName.setText(contact.get(ContactFragment.KEY_CONTACT_NAME));
contactNum.setText(contact.get(ContactFragment.KEY_CONTACT_NUM));
return vi;
}
How can I display multiple numbers under one contact if they have multiple numbers?
Thanks
Figured out the answer. I just added the position number to the phone number I had. When adding the number to the LazyAdapter I looped through the array list and added the numbers like so:
ContactActivity
public LazyAdapter getContactList(){
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
map = new HashMap<String, String>();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
map.put(KEY_CONTACT_NAME, name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneType = "";
int type = pCur.getInt(pCur.getColumnIndexOrThrow(Phone.TYPE));
switch (type){
case Phone.TYPE_HOME:
phoneType = "Home";
break;
case Phone.TYPE_MOBILE:
phoneType = "Mobile";
break;
case Phone.TYPE_WORK:
phoneType = "Work";
break;
case Phone.TYPE_FAX_HOME:
phoneType = "Home Fax";
break;
case Phone.TYPE_FAX_WORK:
phoneType = "Work Fax";
break;
default:
phoneType = "Other";
break;
}
String phoneNo = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
map.put(KEY_CONTACT_NUM + pCur.getPosition(), phoneType + ": " + phoneNo);
}
contactList.add(map);
pCur.close();
}
}
}
adapter = new LazyAdapter(getActivity(), contactList, "Contact");
return adapter;
}
Lazy adapter
TextView contactName = (TextView)vi.findViewById(R.id.contactName); // name
TextView contactNum = (TextView)vi.findViewById(R.id.contactNum); // number
HashMap<String, String> contact = new HashMap<String, String>();
contact = data.get(position);
// Setting all values in listview
contactName.setText(contact.get(ContactFragment.KEY_CONTACT_NAME));
for(int i = 0; i < (contact.size() - 1); i++){
contactNum.setText(contactNum.getText() + contact.get(ContactFragment.KEY_CONTACT_NUM + i) + "\n");
}