王学岗ListView和源码解析(二)
2016-03-26 13:49
429 查看
在本文中我们介绍ListView第二个的适配器——SimpleAdapter;
与ArrayAdapter只能显示单列的不同,SimpleAdapter可以显示多列。
下面我们使用SimpleAdapter在ListView中显示一个人的信息,包括name,number。
下面看下源码:
源码解析:与arraydapter极为类似,也是BaseAdapter的子类
我们直接看getView方法,其他的方法参考(一);
多了一行代码
我们仔细来分析
与ArrayAdapter只能显示单列的不同,SimpleAdapter可以显示多列。
下面我们使用SimpleAdapter在ListView中显示一个人的信息,包括name,number。
下面看下源码:
package com.example.listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView lv_list; private List<Map<String, String>> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_list = (ListView) findViewById(R.id.lv_list); data = new ArrayList<Map<String, String>>(); intData(); // 创建适配器 SimpleAdapter adapter = new SimpleAdapter(this,// 上下文,不多解释 data,// 填充到listView里面的数据 R.layout.list_item,// 每一行的布局文件 new String[] {"name","number"}, //根据每一行的map里面的key,得到里面的值 new int[] {R.id.tv_name,R.id.tv_number}//根据ID,去吧每一个数据填充到对应的个控件上 ); lv_list.setAdapter(adapter); } // 初始化数据 private void intData() { for (int i = 0; i < 150; i++) { Map<String, String> item = new HashMap<String, String>(); item.put("name", "周瑜帅" + "-" + i); item.put("number", String.valueOf(36+i)); data.add(item); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_name" android:layout_width="120dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_number" android:layout_width="140dp" android:layout_height="wrap_content" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ListView android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
源码解析:与arraydapter极为类似,也是BaseAdapter的子类
我们直接看getView方法,其他的方法参考(一);
public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View v; if (convertView == null) { v = mInflater.inflate(resource, parent, false); } else { v = convertView; } bindView(position, v); return v; }
多了一行代码
bindView(position, v);
我们仔细来分析
private void bindView(int position, View view) { final Map dataSet = mData.get(position);//根据行号,得到对应的map if (dataSet == null) { return; } final ViewBinder binder = mViewBinder; final String[] from = mFrom; final int[] to = mTo; final int count = to.length; for (int i = 0; i < count; i++) { final View v = view.findViewById(to[i]);//根据对应的ID找到对应的控件 if (v != null) { final Object data = dataSet.get(from[i]);//根据K(name,number)的值拿到相应的数据 String text = data == null ? "" : data.toString(); if (text == null) { text = ""; } boolean bound = false; if (binder != null) { bound = binder.setViewValue(v, data, text); } if (!bound) { if (v instanceof Checkable) {//对控件的类型进行判断 if (data instanceof Boolean) { ((Checkable) v).setChecked((Boolean) data); } else if (v instanceof TextView) { // Note: keep the instanceof TextView check at the bottom of these // ifs since a lot of views are TextViews (e.g. CheckBoxes). setViewText((TextView) v, text); } else { throw new IllegalStateException(v.getClass().getName() + " should be bound to a Boolean, not a " + (data == null ? "<unknown type>" : data.getClass())); } } else if (v instanceof TextView) { // Note: keep the instanceof TextView check at the bottom of these // ifs since a lot of views are TextViews (e.g. CheckBoxes). setViewText((TextView) v, text);//调用该方法赋值 } else if (v instanceof ImageView) { if (data instanceof Integer) { setViewImage((ImageView) v, (Integer) data); } else { setViewImage((ImageView) v, text); } } else { throw new IllegalStateException(v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleAdapter"); } } } } }
相关文章推荐
- 完美实现Android ListView中的TextView的跑马灯效果
- android上改变listView的选中颜色
- Delphi7中Listview的常用功能汇总
- Delphi控件ListView的属性及使用方法详解
- android中ListView数据刷新时的同步方法
- Android提高之ListView实现自适应表格的方法
- Android中实现水平滑动(横向滑动)ListView示例
- C#实现ListView选中项向上或向下移动的方法
- Listview加载的性能优化是如何实现的
- C#实现listview Group收缩扩展的方法
- C# listview添加combobox到单元格的实现代码
- ListView 百分比进度条(delphi版)
- Android listview多视图嵌套多视图
- ListView Adapter优化 实例
- Android用ListView显示SDCard文件列表的小例子
- Adapter实现ListView带多选框等状态的自定义控件的注意事项
- asp.net ListView 数据绑定
- Android之ScrollView嵌套ListView和GridView冲突的解决方法
- android ListView和ProgressBar(进度条控件)的使用方法
- Android实现ListView异步加载图片的方法