【问题标题】:@android:id/empty shows empty view, but does not show the ListView when data exists in ListFragment@android:id/empty 显示空视图,但当数据存在于 ListFragment 时不显示 ListView
【发布时间】:2014-06-15 14:02:58
【问题描述】:

我有一个具有以下布局的 ListFragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" >
    </ListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No data" />

</LinearLayout>

最初创建 ListFragment 时,它没有数据,一切都很好。 “无数据”文本视图按预期显示。

但是当我添加内容/数据时,ListFragment 仍保留在“无数据”视图中,并且不会切换到显示行。如果我取出 XML 的 @android:id/empty 部分,那么我可以很好地看到这些行。这意味着这不是输入数据的问题,而是关于实际 XML 文件的问题(我相信)。

有没有办法解决这个问题?

更新: 添加到列表时调用 notifyDataSetChanged() 解决了这个问题!

【问题讨论】:

  • 您是否检查过您的适配器在 getCount() 上返回 0 吗?

标签: android android-layout android-fragments android-listview


【解决方案1】:

如果您将此代码添加到片段中,在列表视图上设置适配器会怎么样? list.setEmptyView(findViewById(android.R.id.empty)) ?

【讨论】:

  • 在这种情况下list 是什么?
  • 我错过了您使用的 ListFragment,它应该处理空视图,但在这种情况下,它将是片段内的 getListView()。
  • 如果这没有做任何事情,也许您在添加新数据时忘记调用 notifyDataSetChanged()?
  • 调用 notifyDataSetChanged() 解决了这个问题!谢谢! :)
猜你喜欢
  • 2011-04-15
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2019-11-11
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
相关资源
最近更新 更多