0

我想从 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);

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

提前致谢!

4

1 回答 1

0

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

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();
于 2012-09-03T13:13:25.413 回答