How do I emulate the behavior of @android:id/empty in an Activity that is not a ListActivity?
According to the ListActivity
documentation, when @android:id/empty
is defined, it will show a message instead of a ListView
indicating that there is no data in the list. My Activity subclasses a custom Activity that I wrote, so it cannot simply subclass a ListActivity on its own.
I have attempted to do this in xml to try to emulate this behavior. Here is my current layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_ali开发者_运维技巧gnParentTop="true"
android:text="@string/titleReviewData"
android:gravity="center" android:textSize="24sp"
android:layout_margin="10dp" />
<ListView android:id="@+id/reviewList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2" />
<TextView android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="No data" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/textReviseButton"
android:onClick="revise" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/textSubmitButton"
android:onClick="submit" />
</LinearLayout>
The layout is supposed to show a TextView
title, the ListView
or empty message under it, and two buttons stuck to the bottom of the layout. Unfortunately, the addition of the empty message is not rendered properly.
The only code I have in the corresponding Activity at the moment is code that sets an adapter for @id/reviewList
and lots of code to open dialog boxes. How do I do this?
In your activity code you can set any view to act as the empty view by using listview.setEmptyView(emptyView). It does not need to have the specific id: @android:id/empty. You can either declare the empty view in your XML document or create it in Java. After you have the view call setEmptyView(emptyView)
精彩评论