【问题标题】:Getting the displayed name, but not the number in Android Contact List获取显示的姓名,但不是 Android 联系人列表中的号码
【发布时间】:2012-09-03 21:13:25
【问题描述】:

我想从 Android 联系人列表中获取数据。 我得到了 display_name,但没有得到数字。 我使用了这段代码:

while(people.moveToNext()){
            try{
                int nameFieldColumnIndex = people.getColumnIndex(Phone.DISPLAY_NAME);
                String name = people.getString(nameFieldColumnIndex);
                try{
                    int numberFieldColumnIndex = people.getColumnIndex(Phone.NORMALIZED_NUMBER);
                    String number = people.getString(numberFieldColumnIndex);
                    HashMap<String,String> contactMap=new HashMap<String, String>();
                    contactMap.put("name", name); // per la chiave image, inseriamo la risorsa dell immagine
                    contactMap.put("number",number); // per la chiave name,l'informazine sul nome
                    data.add(contactMap);  //aggiungiamo la mappa di valori alla sorgente dati
                }catch(IllegalStateException e){e.printStackTrace();}

我试过了:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};

        Cursor people = getContentResolver().query(uri, projection, null, null, null);

但结果是一样的,我得到了显示的名字,但没有得到数字。 我读了一些有用的帖子,但我没有完成。 有什么建议吗?

提前致谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    自己解决。 我需要编写一个单独的查询来获取与每个联系人关联的号码:

    ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    
                if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                     if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
    
                             if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                                 Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                                while (pCur.moveToNext()) {
                                    number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    HashMap<String,String> contactMap=new HashMap<String, String>();
                                    contactMap.put("name", name); // per la chiave image, inseriamo la risorsa dell immagine
                                    contactMap.put("number",number); // per la chiave name,l'informazine sul nome
                                    data.add(contactMap);  //aggiungiamo la mappa di valori alla sorgente dati
                                } 
                                pCur.close();
                            }
                }
                }
    
        }   
            cur.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多