1

我有一个附加了自定义延迟适配器的 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;
}

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

谢谢

4

1 回答 1

4

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

联系活动

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");
        }
于 2013-08-14T14:09:35.610 回答