一个联系人下的多个号码

时间:2013-08-12 17:59:11

标签: android android-contacts android-lazyadapter

我有一个附带自定义Lazy Adapter的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;
}

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

由于

1 个答案:

答案 0 :(得分:4)

找出答案。我刚把位置号添加到我的电话号码中。将数字添加到LazyAdapter时,我循环遍历数组列表并添加如下数字:

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

延迟适配器

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