a common use of presenting data to users on a mobile platform is to populate the data in a list, and one thing to note at this point is:
Original address: (http://blog.csdn.net/vector_yi/article/details/24936163)
How do I deal with empty data that needs to be filled? ListView and other inherited fromAdapterviewclasses have a simple way to handle situations like this:Setemptyview (View). When the adapter of the ListView is empty or the adapter IsEmpty () method returns True, it will draw the emptyview of the setting.
For a chestnut, if we need to create an app to manage our backlog, our main page will be a ListView to show these to-dos. And when we first load this app, the backlog must be empty. At this point we can use an image or a descriptive narrative to express "no Todo". Look at the XML layout file:
<framelayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical "> <listview Android:id = "@+id/my_list_view" android:layout_width = "Fill_parent" android:layout_height = "Fill_parent"/ > <imageview android:id = "@+id/empty_view" android:layout_width = "Fill_parent" android: Layout_height = "Fill_parent" android:src = "@drawable/empty_view"/></framelayout>
Then look at your own defined Drawable/empty_view file:
<shape xmlns:android = "http://schemas.android.com/apk/res/android" android:shape= "Rectangle" > < Solid android:color= "#AA00FF00"/></shape>
is a shape that is defined by itself and is displayed when the ListView has no data.
Finally, look at the mainactivity file:
public class Mainactivity extends Activity { private ListView mlistview; @Override public void OnCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (R. layout. Main); Mlistview = (ListView) Findviewbyid (R. Id. my_list_view); Mlistview. Setemptyview (Findviewbyid (R. Id. Empty_view)); /*string[] strs=new string[]{"1", "2"}; Arrayadapter<string> adapter=new arrayadapter<string> (this, Android. R.LAYOUT.SIMPLE_LIST_ITEM_1,STRS); Mlistview.setadapter (adapter); */ }}
creates only one ListView and sets the ImageView created in Emptyview for Main.xml. The code in the gaze is used to test when the ListView has data, Emptyview will not be displayed.
of course, you can use Viewstub as a emptyview, using viewstub to delay loading the view to make sure it won't be rendered without the need to show Emptyview. As for the use of viewstub, I have already described in my previous post, "delayed loading and avoidance of repeated rendering".