如何使用一个光标或投影检索联系人的电话号码

时间:2014-01-10 18:07:51

标签: android listview android-contentprovider android-contacts

我正在编写一个活动,其中包含ListFragment列出我的联系人的图片,姓名和电话号码。我在http://developer.android.com/training/contacts-provider/retrieve-names.html

找到了一个很好的培训课程

但是,演示代码似乎没有显示每个联系人的电话号码。主要查找代码是:

public interface ContactsQuery {

        // An identifier for the loader
        final static int QUERY_ID = 1;

        // A content URI for the Contacts table
        final static Uri CONTENT_URI = Contacts.CONTENT_URI;

        // The search/filter query Uri
        final static Uri FILTER_URI = Contacts.CONTENT_FILTER_URI;

        // The selection clause for the CursorLoader query. The search criteria
        // defined here
        // restrict results to contacts that have a display name and are linked
        // to visible groups.
        // Notice that the search on the string provided by the user is
        // implemented by appending
        // the search string to CONTENT_FILTER_URI.
        @SuppressLint("InlinedApi")
        final static String SELECTION = (Contacts.DISPLAY_NAME_PRIMARY)
                + "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";

        // The desired sort order for the returned Cursor. In Android 3.0 and
        // later, the primary
        // sort key allows for localization. In earlier versions. use the
        // display name as the sort
        // key.
        @SuppressLint("InlinedApi")
        final static String SORT_ORDER = Contacts.SORT_KEY_PRIMARY;

        @SuppressLint("InlinedApi")
        final static String[] PROJECTION = {

                // 行ID
                Contacts._ID,
                // Given a contact's current _ID value and LOOKUP_KEY, the
                // Contacts Provider can generate a "permanent" contact URI.
                Contacts.LOOKUP_KEY,

                // Name
                Contacts.DISPLAY_NAME_PRIMARY,

                // Photo
                Contacts.PHOTO_THUMBNAIL_URI,

                // The sort order column for the returned Cursor, used by the
                // AlphabetIndexer
                SORT_ORDER, };

        // The query column numbers which map to each value in the projection
        final static int ID = 0;
        final static int LOOKUP_KEY = 1;
        final static int DISPLAY_NAME = 2;
        final static int PHOTO_THUMBNAIL_DATA = 3;
        final static int SORT_KEY = 4;
    }

然后它将值绑定到ListItem的视图,如下所示:

public void bindView(View view, Context context, Cursor cursor) {
            // Gets handles to individual view resources
            final ViewHolder holder = (ViewHolder) view.getTag();
            final String photoUri = cursor
                    .getString(ContactsQuery.PHOTO_THUMBNAIL_DATA);
            final String displayName = cursor
                    .getString(ContactsQuery.DISPLAY_NAME);
            holder.text1.setText(displayName);
            int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            String contactId = cursor.getString(idColumn);
            }

我已尝试使用以下代码获取联系人的电话号码,但我找不到像ContactsQuery.XXX这样的参数。

final String displayPhoneNumber = cursor.getString(parameter);

我知道检索电话号码的另一种方法是这样写,但由于第二个光标的使用,它确实降低了ListView的性能:

String[] projection1 = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID };
        final String order = "sort_key asc"; 
        Cursor phones = ContactsPickerActivity.this.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection1, null, null, order);
        String resultNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));;

我可以采取其他任何方法来解决这个问题吗?

0 个答案:

没有答案