50个Android开发技巧(24 处理ListView数据为空的情况)
2015-01-27 10:53
483 查看
在移动平台上为用户展示数据的一个常用方法是将数据填充进一个List内,而此时需要注意的一点就是:
原文地址:(/article/1336210.html)
如何处理需要填充的数据为空的情况?
ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:setEmptyView(View)。
当ListView的Adapter为空或者Adapter的isEmpty()方法返回true的时候,它将会把设置的emptyview绘制出来。
举个栗子,假设我们需要创建一个应用来管理我们的待办事项,我们的主页面将会是一个用来展示这些待办事项的ListView。
而当我们第一次载入进这个应用时,待办事项必然为空。此时我们就可以利用一个图片或者一段描述性的话来表达“无待办事项”。
看看XML布局文件:
<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文件:
<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]
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);*/
}
}
[align=left] 仅仅创建一个ListView并设置了EmptyView为main.xml中创建的ImageView。注释内的代码用来测试当ListView有数据时,emptyview会不会显示。[/align]
[align=left] [/align]
[align=left] 当然,你可以利用ViewStub来作为EmptyView,利用ViewStub可以延迟加载视图,确保在不需要显示EmptyView的时候它不会被渲染。关于ViewStub的用法,我在之前的博文《延迟加载和避免重复渲染》已进行过叙述。[/align]
原文地址:(/article/1336210.html)
如何处理需要填充的数据为空的情况?
ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:setEmptyView(View)。
当ListView的Adapter为空或者Adapter的isEmpty()方法返回true的时候,它将会把设置的emptyview绘制出来。
举个栗子,假设我们需要创建一个应用来管理我们的待办事项,我们的主页面将会是一个用来展示这些待办事项的ListView。
而当我们第一次载入进这个应用时,待办事项必然为空。此时我们就可以利用一个图片或者一段描述性的话来表达“无待办事项”。
看看XML布局文件:
<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文件:
<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]
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);*/
}
}
[align=left] 仅仅创建一个ListView并设置了EmptyView为main.xml中创建的ImageView。注释内的代码用来测试当ListView有数据时,emptyview会不会显示。[/align]
[align=left] [/align]
[align=left] 当然,你可以利用ViewStub来作为EmptyView,利用ViewStub可以延迟加载视图,确保在不需要显示EmptyView的时候它不会被渲染。关于ViewStub的用法,我在之前的博文《延迟加载和避免重复渲染》已进行过叙述。[/align]
相关文章推荐
- 50个Android开发技巧(24 处理ListView数据为空的情况)
- 50个Android开发技巧(24 处理ListView数据为空的情况)
- 50个Android开发技巧(24 处理ListView数据为空的情况)
- 50个Android开发技巧(24 处理ListView数据为空的情况)
- Android开发入门之采用ListView实现数据列表显示
- [原]Android开发技巧--ListView
- Android应用开发之(图片处理技巧一)
- Android开发之ListView列数据的绑定
- Android开发中在ListView中经常有Item的点击事件及Button或ImageButton等需要被点击的事件之间的处理方法
- android开发中难免遇到listview刷新数据出现异常
- 安卓程序代写 网上程序代写[原]Android开发技巧--ListView
- Android应用开发之(图片处理技巧一)
- Android应用开发---listview长按删除该Item数据并删除所在数据库的记录
- Android开发之使用DefaultHandler处理XML数据
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
- 【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
- Android初级开发第七讲--特效和数据传递处理
- Android应用开发之(图片处理技巧一)
- android开发--详解ListView,动态添加,删除Adapter中的数据项
- android开发中难免遇到listview刷新数据出现异常