我想从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);
但结果是一样的,我得到的是显示的名字而不是数字。 我读了一些有用的帖子,但我没有完成。 建议?
提前致谢!
答案 0 :(得分: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();