ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:
setEmptyView(View)。
当ListView的Adapter为空或者Adapter的isEmpty()方法返回true的时候,它将会把设置的emptyview绘制出来。
1. <FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"
2. android:layout_width= "fill_parent"
3. android:layout_height= "fill_parent"
4. android:orientation= "vertical" >
5.
6. <ListView
7. android:id ="@+id/my_list_view"
8. android:layout_width ="fill_parent"
9. android:layout_height ="fill_parent" />
10.
11. <ImageView
12. android:id ="@+id/empty_view"
13. android:layout_width ="fill_parent"
14. android:layout_height ="fill_parent"
15. android:src ="@drawable/empty_view" />
16.
17. </FrameLayout>
仅仅创建一个ListView并设置了EmptyView为main.xml中创建的ImageView。注释内的代码用来测试当ListView有数据时,emptyview会不会显示。