SimpleCursorAdapter处理空光标

M.E*_*.E. 1 android simplecursoradapter

我正在使用自定义的SimpleCursorAdapter.如果在数据库/游标中找不到任何条目,则目前我的列表保持为空.现在,如果游标/数据库中没有条目,我想在列表中显示一条消息.我该如何处理这个事件?

Bar*_*ill 5

只需将您想要显示的视图放在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!" />
Run Code Online (Sandbox Code Playgroud)

然后在Activity的onCreate()方法中,在listView对象上调用setEmptyView().ListView应该处理显示/隐藏视图对象,具体取决于列表是否为空.

    View empty = findViewById(R.id.emptyListElem);      
    listView.setEmptyView(empty);
Run Code Online (Sandbox Code Playgroud)