50 Android Development Tips (24 handles the case of the ListView data being empty)

Source: Internet
Author: User

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".


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.