在应用程序中,经常会用ListView以列表的方式来显示数据,那么下面就开始对ListView进行简单的介绍。
列表显示需要三个要素:
- ListView
- 适配器
- 数据
其中,适配器是将数据显示到ListView的桥梁,使用好适配器是重中之重
用于显示ListView的适配器有ArrayAdapter、SimpleAdapter、SimpleCursorAdapter和BaseAdapter等。
ArrayAdapter
/**
* Context context:上下文
* int resource:布局文件(可选,使用自己定义的xml布局中的控件时指定)
* int textViewResourceId:布局资源内的组件id
* T[]或List<T> objects:数据
*/
ArrayAdapter adapter = new ArrayAdapter<T>(context, [resource,] textViewResourceId, objects);
例子:
// 字符串数组作为数据
String [] data= {"Ada", "Ben", "Candy", "David", "Ella"} ;
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_2, data);
listView.setAdapter(adapter);
// List数据
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_2, getData());
listView.setAdapter(adapter);
private List<String> getData(){
List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
return data;
}
SimpleAdapter
/**
* Context context:上下文
* List<? extends Map<String, ?>> data:数据
* int resource:布局文件
* String[] from:map数据的名字
* int[] to:对应布局资源内组件的id
*/
SimpleAdapter adapter = new SimpleAdapter(context, data, resource, from, to) ;
例子:
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String[]{"title", "icon"}, new int[]{R.id.title, R.id.icon});
listView.setAdapter(adapter);
private List<Map<String, Object>> getData(){
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "摩托罗拉");
map.put("icon", R.drawable.icon1);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "诺基亚");
map.put("icon", R.drawable.icon2);
list.add(map);
map = new HashMa<String, Object>();
map.put("title", "小米");
map.put("icon", R.drawable.icon3);
list.add(map);
return list;
}
SimpleCursorAdapter
/**
* Context context:上下文
* int layout:布局文件
* Cursor c:数据库的游标
* String[] from:数据库表中的字段数组
* int[] to:对应布局文件内组件的id
*/
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, layout, c, from, to)
例子:
Cursor cursor = db.rawQuery("select * from info", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.simplecursor, cursor, new String[]{"title", "icon"}, new int[]{R.id.title, R.id.icon});
listView.setAdapter(adapter);
数据库表:
| _id | title | icon |
| 1 | 摩托罗拉 | R.drawable.icon1 |
| 2 | 小米2s | R.drawable.icon2 |
| 3 | 诺基亚 | R.drawable.icon3 |
BaseAdapter
在这里就不把原始和优化后的做对比了,直接写优化后的BaseAdapter
// ...忽略novels的来源
NovelsAdapter adapter = new NovelsAdapter(this, novels);
listView.setAdapter(adapter);
public class NovelsAdapter extends BaseAdapter {
private Context context;
private List <NovelInfo> novels; //设置数据
public NovelsAdapter(Context context, List <NovelInfo> novels) {
this. context = context ;
this. novels = novels ;
}
@Override
public int getCount() {
return novels.size();
}
@Override
public Object getItem(int position) {
return novels.get(position) ;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null){
// 获取布局文件中的控件
view = LayoutInflater.from(context).inflate(R.layout.novel_item, null) ;
holder = new ViewHolder() ;
holder.name= (TextView) view.findViewById(R.id.tv_novel_name) ;
holder.author = (TextView) view.findViewById(R.id.tv_novel_author) ;
holder.type = (TextView) view.findViewById(R.id. tv_novel_type) ;
view.setTag(holder); //打一个标记
} else{
holder = (ViewHolder) view.getTag(); //取出标记
}
// 为控件绑定数据
NovelInfo novel = (NovelInfo) getItem(position) ;
holder.name.setText(novel.name) ;
holder.author.setText(novel.author) ;
holder.type.setText (novel.typeName) ;
return view;
}
static class ViewHolder{
TextView name;
TextView author;
TextView type;
}
}
NovelInfo.java:
public class NovelInfo {
public String name;// 小说名称
public String author;// 小说作者
public String type;// 小说类型
}
还有就是尽量避免在getView()中进行耗时操作,应使用异步的方式为item的布局设置数据,如果是在需要同步,则要在Adapter初始化时将数据加载好,然后再在getView()中绑定数据。
其实现在ListView已经算是过时了,现在的大神们都是使用RecyclerView。RecyclerView的功能非常强大,能实现ListView和GridView还有瀑布流等效果,从ListView转换为GridView只需几行代码。
网上有很多大神们分享的关于RecyclerView的案例,大家可以google搜索几篇文章来学习