我正在使用自定义 SimpleCursorAdapter。目前,如果在数据库/光标中找不到任何条目,我的列表仍然是空的。现在,如果光标/数据库中没有条目,我想在列表中显示一条消息。我该如何处理这个事件?
问问题
2602 次
2 回答
6
只需将要显示的视图放在 XML 中,并给它任何你喜欢的 id,例如:
<ListView
android:id="@+id/myList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- view to be shown/hidden on empty list above -->
<TextView
android:id="@+id/emptyListElem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Nothing to show!" />
然后在 Activity 的onCreate()方法中,在 listView 对象上调用setEmptyView() 。ListView 应该处理显示/隐藏您的视图对象,具体取决于列表是否为空。
View empty = findViewById(R.id.emptyListElem);
listView.setEmptyView(empty);
于 2012-02-15T16:19:29.680 回答
3
如果您的 ListView 以 xml 表示,您可以添加一个具有特定 android:id 的 TextView,如果列表为空,它将自动显示您的消息,如下所示:
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" />
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:text="No Data exist"
android:textSize="16dip"
android:textColor="@color/black"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
您必须使用 @id/android:empty 才能使其工作。
于 2011-01-30T23:29:46.477 回答