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

Source: Internet
Author: User

a common way to present data to users on a mobile platform is to populate the data in a list, and one thing to be aware of at this point is:
Original address: (http://blog.csdn.net/vector_yi/article/details/24936163)
How do I handle a case where the data that needs to be populated is empty? ListView and other inherited fromAdapterviewclasses have a simple way to handle this situation: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, let's say we need to create an app to manage our to-dos, and our main page will be a ListView to show these to-dos. When we first load into this application, the backlog must be empty.     At this point we can use a picture or a descriptive word 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 the custom Drawable/empty_view file:
<shape xmlns:android = "http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >    < Solid android:color= "#AA00FF00"/></shape>
is a custom shape that shows up 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); */     }}

just create a ListView and set the ImageView created in Emptyview to Main.xml. The code in the comment is used to test whether the Emptyview will be displayed when the ListView has data.
of course, you can use Viewstub as a emptyview, and viewstub can delay loading the view to make sure it won't be rendered without the need to display Emptyview. With regard to the usage of viewstub, I have already described in my previous post, "lazy loading and avoiding 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.