现在的位置: 首页 > 综合 > 正文

ListView设置EmptyView的方法

2013年04月05日 ⁄ 综合 ⁄ 共 2813字 ⁄ 字号 评论关闭

原文:http://blog.csdn.net/yuanzeyao2008/article/details/8153365

我们在使用ListView展示数据时,如何需要展示的数据集为空,那么就会显示一个黑屏,为了解决该问题,ListView有一个方法setEmptyView,当数据集为空时,就显示设置的这个界面。

现在分两种情况来分析这个问题:

如果你的Activity继承ListActivity:

这种情况相对简单,

定义非空时的xml

  1. <LinearLayout  
  2.   xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent">  
  6.     
  7.     <ListView   
  8.        android:id="@android:id/list"  
  9.        android:layout_width="fill_parent"   
  10.        android:layout_height="fill_parent"   
  11.     ></ListView>  
  12.       
  13.     <ViewStub android:id="@android:id/empty"   
  14.            android:layout_width="fill_parent"   
  15.        android:layout_height="fill_parent"   
  16.             android:layout_gravity="center"   
  17.             android:layout="@layout/emptyview"   
  18.     />          
  19. </LinearLayout>  

定义emptyview.xml

  1. <LinearLayout 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.     <Button android:id="@+id/btn_emptyview"  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"  
  8.         android:text="EmptyView视图"  
  9.         android:textSize="20pt"  
  10.         />"  
  11.       
  12.   
  13. </LinearLayout>  


对于这种情况,只需要这两个xml就可以完成

 

如果使用普通的Activity完成

定义非空时的xml:

  1. <LinearLayout  
  2.   xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent">  
  6.     
  7.     <ListView   
  8.         android:id="@+id/list"  
  9.         android:layout_width="fill_parent"   
  10.        android:layout_height="fill_parent"   
  11.     ></ListView>  
  12.       
  13.     <ViewStub android:id="@+id/empty"   
  14.        android:layout_width="fill_parent"   
  15.        android:layout_height="fill_parent"   
  16.        android:layout_gravity="center"   
  17.        android:layout="@layout/emptyview"   
  18.     />       
  19.       
  20.       
  21. </LinearLayout>  

定义空时的xml和上面一样

区别在于Actiivty中的代码

  1. public class SecondActivity extends Activity  
  2. {  
  3.     //private static final String[]items={"A","N","C"};  
  4.     private static final String[]items={};  
  5.     private ListView list;  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState)  
  8.     {  
  9.         // TODO Auto-generated method stub  
  10.         super.onCreate(savedInstanceState);  
  11.         this.setContentView(R.layout.noempty);  
  12.         ArrayAdapter<String>adaptr=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);  
  13.         list=(ListView)this.findViewById(R.id.list);  
  14.         list.setAdapter(adaptr);  
  15.         ViewStub mViewStub = (ViewStub)findViewById(R.id.empty);    
  16.   
  17.         list.setEmptyView(mViewStub);            
  18.   
  19.     }  
  20. }  

抱歉!评论已关闭.