我有一个附加了自定义延迟适配器的 ListView。我从手机中提取联系人并将它们显示在我的列表中。我在处理多个数字时遇到问题。如果一个联系人有多个不同类型的号码,它们将显示为不同的联系人,如下所示:
这是获取我的联系人列表的代码:
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;
}
这是我的懒惰适配器
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;
}
如果一个联系人有多个号码,如何在一个联系人下显示多个号码?
谢谢