【问题标题】:How can this SimpleCursorAdapter work fine?这个 SimpleCursorAdapter 怎么能正常工作?
【发布时间】:2014-09-03 08:59:13
【问题描述】:

所以我有一个老问题:代码可以正常工作,但不应该。谁能猜出这怎么可能?

我正在显示联系人列表,其中包含每个联系人的照片缩略图。返回的光标数据没有任何照片缩略图数据,但每个联系人的缩略图都正确显示。

列表项视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/userpic"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:contentDescription="Userpic"
        />

    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:clickable="true"
        android:textSize="24sp"
        android:padding="12dp"
        android:layout_gravity="center_vertical"
        />
</LinearLayout>

主要代码

private static class CursorAdapter extends SimpleCursorAdapter {
    private static final String[] FROM = new String[]{
            Contacts.DISPLAY_NAME_PRIMARY,
            Contacts.PHOTO_THUMBNAIL_URI
    };
    private static final int[] TO = new int[]{
            android.R.id.text1,
            R.id.userpic
    };

    public CursorAdapter(Activity activity, Cursor c) {
        super(activity, R.layout.contacts_item, c, FROM, TO, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
         // Here I logged cursor contents and am sure that thumbnail column data is null.
        dumpCursorCurrent(cursor);

        View view = super.newView(context, cursor, parent);

        int columnIndex = cursor.getColumnIndexOrThrow(Contacts.PHOTO_THUMBNAIL_URI);
        String thumbnailUriString = cursor.getString(columnIndex);
        if (thumbnailUriString != null) {
            throw new RuntimeException("Never comes here");
        }
        return view;
    }
}

创建光标的代码:

    ContentResolver contentResolver = getActivity().getContentResolver();
    Cursor c = contentResolver.query(
            Contacts.CONTENT_URI,
            projection,
            null,
            null,
            null
    );

因此,对于每个拥有照片的联系人,列表都有正确的张照片。

我调试了 SimpleCursorAdapter 代码,它不应该工作,它调用imageView.setImageURI(Uri.parse("")),并且自然地为每一行记录一个警告:

09-03 01:47:20.023 27814-27814/me.lazerka.mf.android E/BitmapFactory﹕ 无法解码流:java.io.FileNotFoundException:/:打开 失败:EISDIR(是一个目录)09-03 01:47:20.023

27814-27814/me.lazerka.mf.android I/System.out:resolveUri 失败 错误的位图 uri:

那么谁为每个 ImageView 设置了正确的 imageURI,它在哪里(游标始终为 null)?

【问题讨论】:

  • 你确定游标到处都有空值吗?试过 DatabaseUtils.dumpCursor()?
  • 感谢DatabaseUtils 的建议,它表明确实没有空值。诀窍在于CursorAdapter#getView()——我有一个带有空照片的项目,我正在添加一个带有非空照片的项目。但在 getView() 中,它重用 现有视图,而不调用 newView() 我放置断点的位置。所以它用我之前已经拥有的空照片为那个联系人调用 newView。如果您关心积分,请随时发布答案,我会接受,谢谢。
  • 很高兴听到它现在可以使用

标签: android android-layout


【解决方案1】:

问题隐藏在 SimpleCursorAdapter 的实现中:它重用了视图,并且只为最后一个列表项调用了 newView()。因此,当我添加联系人时,它会重用以前的视图,将其设置为正确的照片,并且没有调用 newView(),因此我的断点没有触发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2021-01-30
    • 2014-11-13
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多