【问题标题】:Display Starred Contacts with Pictures on custom ListView在自定义 ListView 上显示带有图片的已加星标的联系人
【发布时间】:2013-05-27 00:15:13
【问题描述】:

我正在尝试仅显示带有星标的联系人的列表视图,以及自定义列表视图的图标和名称。到目前为止,我已经设法在没有照片的情况下正确显示它们。当我尝试包含照片时,我遇到了几个错误(因为我尝试了很多不同的方法,这些方法在这里找到)。我最后一次尝试是通过实现 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,大小为6 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 在 android.app.ActivityThread.handleLaunchActivity(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) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 在 dalvik.system.NativeStart.main(本机方法) 引起:android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为6 在 android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) 在 android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 在 android.database.AbstractWindowedCursor.getLong(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 更多

【问题讨论】:

    标签: android contacts photos


    【解决方案1】:

    问题来了

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

    (from 和 to)中的项目数必须相同。

    所以你可以将from改写为

    String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
    

    【讨论】:

    • 我也遇到了同样的错误。 “android.database.CursorIndexOutOfBoundsException:请求索引 -1,大小为 6”。再次将我指向同一行代码
    • 嘿你应该使用另一个适配器.....我正在寻找你的错误并得到了许多与光标适配器相关的问题..
    【解决方案2】:

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

    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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      相关资源
      最近更新 更多