【问题标题】:Android ListView exception on custom item layout自定义项目布局上的 Android ListView 异常
【发布时间】:2014-06-11 17:32:19
【问题描述】:

我正在使用 simpleCursorAdapter 来加载 ListView。

ListView 项由 XML 定义:

<LinearLayout xmlns: android......
    android:id="@+id/listItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/listDate"
        android:layout_width="180dip"
        android:layout_height="wrap_content"
        android:marginBottom="3dip"/>
    <TextView
        android:id="@+id/listName"
        android:layout_width="110dip"
        android:layout_height="wrap_content"
        android:marginBottom="3dip"/>
</LinearLayout>

如果我将该 listItem id 传递给 ListView 的适配器,我会在资源类型 0x12 上得到一个 FATAL EXCEPTION。

如果我通过了 android.R.layout.simple_list_item_1,它不会抛出 FATAL EXCEPTION,但它也不会做我需要做的事情。

所有文档都表明我可以为适配器检索的项目使用自定义布局。这正是我需要做的。

如何将自定义布局传递给 ListView 适配器?

【问题讨论】:

    标签: android android-layout listview android-listview


    【解决方案1】:

    【讨论】:

    • 假设我不想重新实现 SimpleCursorAdapter:SimpleCursorAdapter究竟期望什么作为项目布局?
    • BaseAdapter 将是实现列表视图的最佳选择,请尝试使用它来实现。
    • 我用 newView() 和 bindView() 方法实现了 CursorAdapter。这样可行。非常奇怪的是 SimpleCursorAdaptor 需要在其构造函数中执行此操作所需的一切,并且它的文档使人们相信这是可能的。我会把它写到“SimpleCursorAdaptor 和 SimpleJack:豆荚里的豌豆”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多