註意本例R.layout.list_8.xml 中將ListView 和一個TextView,其中TextView 的id為 @+id/emtpy,放在FrameLayout中,當ListView 為空時,TextView會顯示出來,顯示一行文字“no photos” 表示Listview 為空:
[html]
<!– The frame layout is here since we will be showing either
the empty view or the list view. –>
<FrameLayout
android:layout_width=”match_parent”
android:layout_height=”0dip”
android:layout_weight=”1″ >
<!– Here is the list. Since we are using a ListActivity, we
have to call it “@android:id/list” so ListActivity will
find it –>
<ListView android:id=”@android:id/list”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:drawSelectorOnTop=”false”/>
<!– Here is the view to show if the list is emtpy –>
<TextView android:id=”@+id/empty”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:text=”@string/list_8_no_photos”/>
</FrameLayout>
<!– The frame layout is here since we will be showing either
the empty view or the list view. –>
<FrameLayout
android:layout_width=”match_parent”
android:layout_height=”0dip”
android:layout_weight=”1″ >
<!– Here is the list. Since we are using a ListActivity, we
have to call it “@android:id/list” so ListActivity will
find it –>
<ListView android:id=”@android:id/list”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:drawSelectorOnTop=”false”/>
<!– Here is the view to show if the list is emtpy –>
<TextView android:id=”@+id/empty”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:text=”@string/list_8_no_photos”/>
</FrameLayout>
其實ListActivity提供瞭一個更簡潔的方法為ListView設置為空時顯示的View,可以在Layout使用任意一種View,隻要將其id 設置為”android:id/empty”,則可以用來顯示ListView為空時的UI。
本例可以動態為ListView 隨機添加照片,Clear 可以清空表,註意改變ListView數據源後,要調用notifyDataSetChanged()通知UI更新屏幕.