您的位置:首页 > 其它

如何处理需要填充的数据为空的情况

2017-05-10 19:11 399 查看
如何处理需要填充的数据为空的情况?
   
      ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:setEmptyView(View)。

     当ListView的Adapter为空或者Adapter的isEmpty()方法返回true的时候,它将会把设置的emptyview绘制出来。

     举个栗子,假设我们需要创建一个应用来管理我们的待办事项,我们的主页面将会是一个用来展示这些待办事项的ListView。
     而当我们第一次载入进这个应用时,待办事项必然为空。此时我们就可以利用一个图片或者一段描述性的话来表达“无待办事项”。
     看看XML布局文件:

[html]
view plain
copy





<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" />  
  
lt;/FrameLayout>  

再来看自定义的drawable/empty_view文件:

[html]
view plain
copy





<shape xmlns:android = "http://schemas.android.com/apk/res/android"  
    android:shape= "rectangle" >  
    <solid android:color= "#AA00FF00" />  
</shape>  

[align=left]     是一个自定义的shape,当ListView没数据的时候才展现出来。

[/align]
[align=left]     最后再看MainActivity文件:[/align]

[java]
view plain
copy





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);*/  
     
  }  
  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐