I want to get data from Android contact list.
I got the display_name but not the number.
I used this code:
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();}
and I tried:
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);
but the result is the same, I got the displayed name but not the number.
I read some useful post, but I don t accomplish.
Suggestions?
Thanks in advance!
Solved by myself.
I needed to code a separated query to get the number associated to each contact:
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();
http://stackoverflow.com/questions/12243933/getting-the-displayed-name-but-not-the-number-in-android-contact-list