【问题标题】:Loading contact photos into contact list: random contact photos appear in wrong places将联系人照片加载到联系人列表中:随机联系人照片出现在错误的位置
【发布时间】:2011-11-01 13:34:16
【问题描述】:

我已使用扩展的 SimpleCursorAdapter 将我的联系人加载到列表中,现在我正在尝试加载联系人照片。运行代码时,随机联系人照片会出现在随机联系人旁边,即使对于那些没有照片的人也是如此。为什么不直接获取那些拥有照片的联系人的照片并显示在他们旁边?

代码如下:

public void bindView(View view, Context context, Cursor cursor) {
ImageView photo = (ImageView) findViewById(R.id.photo);
long photoId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));

Bitmap photoBitmap = loadContactPhoto(photoId);
    if (photoBitmap != null) {
        photo.setImageBitmap(photoBitmap);
    }

以及加载ContactPhoto的代码:

public Bitmap loadContactPhoto(long id) {
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id);
    byte[] data = null;
    Cursor cursor = managedQuery(
        contactUri, // Uri
        new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO }, // projection, the contact photo
        ContactsContract.Contacts.PHOTO_ID + "!= 0", // where statement, only if the contact has a photo
        null, null);
    Log.i(LOG_TAG, "cursorCount: " + cursor.getCount()); // returns 1
    if (cursor == null || !cursor.moveToNext()) {           
        return null;
    }
    data = cursor.getBlob(0);
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    return bitmap;
}

【问题讨论】:

  • 您是如何实现“查看视图”的?

标签: java android simplecursoradapter contactscontract


【解决方案1】:

在 bindView() 中,您正在调用:

ImageView photo = (ImageView) findViewById(R.id.photo);

你不应该在视图参数上调用 findViewById() 吗?

ImageView photo = (ImageView) view.findViewById(R.id.photo);

【讨论】:

  • 好的,谢谢!这部分解决了问题——它现在在正确的联系人旁边显示正确的照片。但是我仍然会在没有联系人照片的随机其他联系人旁边获得随机照片。有什么想法吗?
  • 澄清一下,当我开始滚动列表时会发生这种情况。可能是因为 getView 或 newView,我应该也覆盖它们吗?
  • 还发现了另一个罪魁祸首——我的 bindView 正在回收这些行,所以我之前加载的照片随机出现在其他行中。通过将else 语句添加到if (photoBitmap != null) 来修复。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 2017-07-10
  • 1970-01-01
  • 2014-01-14
相关资源
最近更新 更多