gpt4 book ai didi

android - 一个联系人下的多个号码

转载 作者:行者123 更新时间:2023-11-29 16:06:08 24 4
gpt4 key购买 nike

我有一个带有自定义惰性适配器的 ListView。我从手机中提取联系人并将其显示在我的列表中。我在处理多个号码时遇到问题。如果一个联系人有多个不同类型的号码,它们将显示为不同的联系人,如下所示:

enter image description here

这是获取我的联系人列表的代码:

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;
}

如果联系人有多个号码,如何在一个联系人下显示多个号码?

谢谢

最佳答案

找出答案。我只是将职位编号添加到我的电话号码中。将数字添加到 LazyAdapter 时,我遍历数组列表并添加数字,如下所示:

联系 Activity

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;
}

惰性适配器

        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");
}

关于android - 一个联系人下的多个号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18193593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com