开发者

_id and Number not accessing using query : Android

my code is as following

String projection[] = new String[]{ ContactsContract.Contacts._ID,
                                            ContactsContract.Contacts.DISPLAY_NAME };
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, //The URI, using the content:// scheme, for the content to retrieve.
                        projection,             // projection
                        null,               // selection
                        null,               // selectionArgs
                        ContactsContract.Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

I want to add contact id, name and number (MAIN or any one of type) to intent, but problem is that I am unable to access Na开发者_如何学运维me. And another problem is that for each contact id returns "-1". why? Is there any solution?


Instead off supplying the value of projection variable as null in the query method you should try it like this:

String[] projection = new String[] {ContactsContract.Data._ID,
                                    ContactsContract.Data.CONTACT_ID,
                                    ContactsContract.Data.DISPLAY_NAME};

It is clearly written in Android Developer Documentation that:

Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named "_id" or this class will not work.

See here in the documentation:

http://developer.android.com/reference/android/widget/CursorAdapter.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜