开发者

Android Contacts Can't retrieve data

Android Contacts is driving me mad! This code is returning empty cursors, but the contacts exists! Can anyone see what I can't?

        ContentResolver cr = getContentResolver();
        String query = ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = " +pickedID;
        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,query , null, null);
        pCur.moveToFirst();
            while (pCur.moveToNext()) {
                    contactPhone.setText(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
                } 
                pCur.close();

             开发者_如何转开发  query = ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = " +pickedID;
                pCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,query , null, null);
                pCur.moveToFirst();
                        while (pCur.moveToNext()) {
                            contactPhone.setText(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
                        } 
                        pCur.close();


I had the same problem with the null cursor and my problem was that, I had forgotten to add the following line of code in the Manifest.xml file:

uses-permission android:name="android.permission.READ_CONTACTS"


I'm not sure if this is the entire cause of the problem, but you are calling

pCur.moveToFirst();

which moves the cursor to the first entry. Then you immediately call

while (pCur.moveToNext())

which moves the cursor to the second entry. So you are skipping the first entry.

Leave out the pCur.moveToFirst(); and just leave the while loop and see if that helps.


I think the root of your problem is how you initially retrieve the contact list.

 Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,query , null, null);

should be..

Cursor pCur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜