开发者

problem with reading contacts in android

开发者 https://www.devze.com 2023-03-23 18:04 出处:网络
i\'m trying to read contacts with this code but it only gets Contacts but not contacts data.Please tell me am i going on a wrong path or what is wrong with this.

i'm trying to read contacts with this code but it only gets Contacts but not contacts data.Please tell me am i going on a wrong path or what is wrong with this.

Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    while(contacts.moveToNext()) {
       int contactIdColumnIndex = contacts.getColumnIndex(ContactsContract.Contacts._ID);
       long contactId = contacts.getLong(contactIdColumnIndex);
       System.out.println(contactId);

       Cursor c = getContentResolver().query(Data.CONTENT_URI,
                  new String[] {Data._ID, Phone.NUMBER, Phone.TYPE},
                  Data.CONTACT_ID + "=?" + " AND "
                          + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
                  new String[] {String.valueOf(contactId)}, null);
//retrieving data
 while(c.mo开发者_如何转开发veToNext()){
           long Id=c.getLong(0);
           String number=c.getString(1);
           String type=c.getString(2);
           String name=c.getString(3);
           System.out.println(Id);
           System.out.println(number);
           System.out.println(type);
           System.out.println(name);

       }

       c.close();
}
contacts.close();

This runs well when i debug this and it only print ContactIds only 1 2 3 4

but no data...(apologize for long question)


try this code

String number = "";

    ContentResolver cr = 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(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                            new String[]{id}, null);

                    while (pCur.moveToNext()) {
                        for(int i=0;i<pCur.getColumnCount();i++)
                            number = pCur.getString(i);

                    } 
                    pCur.close();
                    pCur = null;
                }

        }
    }
    cur.close();
    cur = null;
    cr = null;
0

精彩评论

暂无评论...
验证码 换一张
取 消