1

我正在尝试仅显示带有星标的联系人的列表视图,以及自定义列表视图的图标和名称。到目前为止,我已经设法在没有照片的情况下正确显示它们。当我试图包含照片时,我遇到了几个错误(因为我尝试了很多不同的方法,这些方法在这里找到)。我最后一次尝试是通过实现 Android Developers "* Displaying the Quick Contact Badg *e" 课程的代码,以下是相关代码:

Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED,
            ContactsContract.Contacts.LOOKUP_KEY,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
    String selection =ContactsContract.Contacts.STARRED + "='1'";

    Cursor cursor = managedQuery(queryUri, projection, selection,null,null);

    int mIdColumn;
    int mLookupKeyColumn;
    Uri mContactUri;

    mIdColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
    // Gets the LOOKUP_KEY index
    mLookupKeyColumn = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);

    mContactUri =
            ContactsContract.Contacts.getLookupUri(
                    cursor.getLong(mIdColumn),
                    cursor.getString(mLookupKeyColumn)
            );


    favIcon.assignContactUri(mContactUri);

    // The column in which to find the thumbnail ID
    int mThumbnailColumn;
/*
 * The thumbnail URI, expressed as a String.
 * Contacts Provider stores URIs as String values.
 */
    String mThumbnailUri;

/*
 * Gets the photo thumbnail column index if
 * platform version >= Honeycomb
 */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mThumbnailColumn =
                cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI);
        // Otherwise, sets the thumbnail column to the _ID column
    } else {
        mThumbnailColumn = mIdColumn;
    }
/*
 * Assuming the current Cursor position is the contact you want,
 * gets the thumbnail ID
 */
    mThumbnailUri = cursor.getString(mThumbnailColumn);

    Bitmap mThumbnail =
            loadContactPhotoThumbnail(mThumbnailUri);
    favIcon.setImageBitmap(mThumbnail);

    String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
    int to[] = new int[]{
            R.id.ivDefContact,
            R.id.tvContactName
    };

    ListAdapter adapter = new SimpleCursorAdapter(
            this,
            R.layout.favs_list_item,
            cursor,
            from,
            to,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);


    final ListView listStarred = (ListView) findViewById(R.id.lvFavs);

    listStarred.setAdapter(adapter);

}

我用上面的代码得到的错误是:

Android.database.CursorIndexOutOfBoundsException: 请求索引 -1,大小为 9(9 是我加星标的联系人的数量)并指向第 85 行,即:

mContactUri =
            ContactsContract.Contacts.getLookupUri(
                    cursor.getLong(mIdColumn),
                    cursor.getString(mLookupKeyColumn)
            );

如果我评论R.id.ivDefContact,将 Activity 与列表视图对齐运行正常并正确显示联系人姓名。所以问题出在照片实现上。我阅读了一些相关的线程,但我无法了解它是如何工作的。

编辑:Logcat 错误:

java.lang.RuntimeException: 无法启动活动 ComponentInfo android.database.CursorIndexOutOfBoundsException: 请求索引 -1,在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 在 android.app.ActivityThread.handleLaunchActivity 的大小为 6 (ActivityThread.java:2357) 在 android.app.ActivityThread.access$600(ActivityThread.java:153) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) 在 android.os.Handler.dispatchMessage(Handler .java:99) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:5226) 在 java.lang.reflect.Method.invokeNative(Native Method) 在com.android.internal.os 上的 java.lang.reflect.Method.invoke(Method.java:511)。ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method) 原因:android.database。 CursorIndexOutOfBoundsException:请求索引 -1,在 android.database.AbstractWindowedCursor.getLong 的 android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 的 android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) 的大小为 6 (AbstractWindowedCursor.java:74) 在 android.database.CursorWrapper.getLong(CursorWrapper.java:106) 在 com.example.DialerActivity.onCreate(DialerActivity.java:85) 在 android.app.Activity.performCreate(Activity.java: 5104) 在 android.app.Instrumentation。callActivityOnCreate(Instrumentation.java:1080) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) ... 11 更多

4

2 回答 2

2

问题就在这里

String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
    int to[] = new int[]{
            R.id.ivDefContact,
            R.id.tvContactName
    }; 

(从和到)中的项目数必须相同。

所以你可以重写from

String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
于 2013-05-31T08:20:44.340 回答
1

我已经设法显示具有上述更改的照片:

Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.LOOKUP_KEY,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED};

    String selection =ContactsContract.Contacts.STARRED + "='1'";

    Cursor cursor = managedQuery(queryUri, projection, selection,null,null);

    long id= cursor.getColumnIndex(ContactsContract.Contacts._ID);

    Bitmap bitmap = loadContactPhoto(getContentResolver(), id);
    if(bitmap!=null){
    favIcon.setImageBitmap(bitmap);
    }
    else{

    }

    String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
    int to[] = new int[]{
            R.id.ivDefContact,
            R.id.tvContactName
    };

    ListAdapter adapter = new SimpleCursorAdapter(
            this,
            R.layout.favs_list_item,
            cursor,
            from,
            to,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);


    final ListView listStarred = (ListView) findViewById(R.id.lvFavs);

    listStarred.setAdapter(adapter);

    public static Bitmap loadContactPhoto(ContentResolver cr, long  id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {

        return null;
    }
    return BitmapFactory.decodeStream(input);
}

现在照片对于有照片的联系人显示正确。

于 2013-05-31T13:13:30.240 回答