zoukankan      html  css  js  c++  java
  • 50个Android开发技巧(24 处理ListView数据为空的情况)

     在移动平台上为用户展示数据的一个常用方法是将数据填充进一个List内,而此时需要注意的一点就是:
    原文地址:(http://blog.csdn.net/vector_yi/article/details/24936163)

              如何处理需要填充的数据为空的情况?
       
          ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:setEmptyView(View)。
         当ListView的Adapter为空或者Adapter的isEmpty()方法返回true的时候,它将会把设置的emptyview绘制出来。
     
         举个栗子,假设我们需要创建一个应用来管理我们的待办事项,我们的主页面将会是一个用来展示这些待办事项的ListView。
         而当我们第一次载入进这个应用时,待办事项必然为空。此时我们就可以利用一个图片或者一段描述性的话来表达“无待办事项”。
         看看XML布局文件:
    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
    2.    android:layout_width"fill_parent"  
    3.    android:layout_height"fill_parent"  
    4.    android:orientation"vertical" >  
    5.   
    6.    <ListView  
    7.        android:id ="@+id/my_list_view"  
    8.        android:layout_width ="fill_parent"  
    9.        android:layout_height ="fill_parent" />  
    10.   
    11.    <ImageView  
    12.        android:id ="@+id/empty_view"  
    13.        android:layout_width ="fill_parent"  
    14.        android:layout_height ="fill_parent"  
    15.        android:src ="@drawable/empty_view" />  
    16.   
    17. lt;/FrameLayout>  

    再来看自定义的drawable/empty_view文件:
    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <shape xmlns:android = "http://schemas.android.com/apk/res/android"  
    2.     android:shape"rectangle" >  
    3.     <solid android:color"#AA00FF00" />  
    4. </shape>  
         是一个自定义的shape,当ListView没数据的时候才展现出来。

         最后再看MainActivity文件:
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. public class MainActivity extends Activity {  
    2.   
    3.   private ListView mListView;  
    4.   
    5.   @Override  
    6.   public void onCreate (Bundle savedInstanceState ) {  
    7.     super. onCreate( savedInstanceState );  
    8.     setContentView (R .layout .main );  
    9.   
    10.     mListView = (ListView ) findViewById (R .id .my_list_view );  
    11.     mListView. setEmptyView (findViewById (R .id .empty_view ));  
    12.     /*String[] strs=new String[]{"1","2"}; 
    13.     ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,strs); 
    14.     mListView.setAdapter(adapter);*/  
    15.      
    16.   }  
    17.   
    18. }  

        仅仅创建一个ListView并设置了EmptyView为main.xml中创建的ImageView。注释内的代码用来测试当ListView有数据时,emptyview会不会显示。
     
         当然,你可以利用ViewStub来作为EmptyView,利用ViewStub可以延迟加载视图,确保在不需要显示EmptyView的时候它不会被渲染。关于ViewStub的用法,我在之前的博文《延迟加载和避免重复渲染》已进行过叙述。
  • 相关阅读:
    RabbitMQ插件安装
    RabbitMQ安装与初始配置
    Spring Boot教程(四十二)LDAP来管理用户信息(2)
    Spring Boot教程(四十一)LDAP来管理用户信息(1)
    Spring Boot教程(四十)使用Flyway来管理数据库版本
    Spring Boot教程(三十九)使用MyBatis注解配置详解(2)
    Spring Boot教程(三十八)使用MyBatis注解配置详解(1)
    Spring Boot教程(三十七)整合MyBatis
    Spring Boot教程(三十六)使用MongoDB数据库(2)
    Spring Boot教程(三十五)使用MongoDB数据库(1)
  • 原文地址:https://www.cnblogs.com/mfryf/p/3705679.html
Copyright © 2011-2022 走看看