listview与适配器
适用范围:‘列表’型数据加载。
示例:BaseAdapter ArrayAdapter SimpleAdapter适配器
中心思想:[ 构造适配器,提供数据(数据,资源文件),绑定listview ]
1.BaseAdapter方式:外加listview_item布局文件
思路:
继承BaseAdapter类,实现内部getCount(?),getView(?)函数;
listview布局,以及listview_item布局
提供适配器数据;
绑定数据适配器;
源码示例
[
public class MainActivity2 extends Activity {
public static final String TAG = "MainActivity";
private List<Dict> list = null;// 数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//listview布局
ListView listview = (ListView) findViewById(R.id.listview);
DictDao2 dao = new DictDao2(this);
// 数据项
list = dao.queryAll();
listview.setAdapter(new MeAdapter());
}
public class MeAdapter extends BaseAdapter {
/**
* listview多少项
*/
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
/**
* 函数返回出去listview_item
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView != null) {
view = convertView;
} else {
// 打气筒
LayoutInflater inflater = MainActivity2.this.getLayoutInflater();
// 打气筒获取xml配置的item项
view = inflater.inflate(R.layout.listview_item, null);
}
TextView tv_word = (TextView) view.findViewById(R.id.tv_word);
TextView tv_detail = (TextView) view.findViewById(R.id.tv_detail);
// 设置item项内部显示文本
Dict dict = list.get(position);
tv_word.setText("单词:" + dict.getWord());
tv_detail.setText("说明:" + dict.getDetail());
return view;
}
}
}
]
2.ArrayAdapter方式:最简单
思路:
构造ArrayAdapter对象,绑定listview
源码示例
[
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//listview布局
ListView listview = (ListView) findViewById(R.id.listview);
String[] textArray = new String[]{"张三1","张三2","张三3","张三4","张三5","张三6"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, textArray);
//listview绑定数据适配器
listview.setAdapter(adapter);
}
}
]
3.SimpleAdapter方式:构造适配器,绑定listview
思路:
构造SimpleAdapter对象,绑定listview
源码示例
[
public class MainActivity extends Activity {
private Map<String, String> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// listview布局
// listview组件
ListView listview = (ListView) findViewById(R.id.listview);
// 参数值
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (int i = 0; i < 6; i++) {
map = new HashMap<String, String>();
map.put("name", "方文山" + i);
map.put("age", "" + 30 + i);
data.add(map);
}
int resource = R.layout.listview_item;
String[] from = new String[] { "name", "age" };
int[] to = new int[] { R.id.tv_name, R.id.tv_age };
// 构造SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(
this, // 上下文
data,// listView绑定的数据
resource,// listview的子条目的布局的id
from,// data数据中的map集合里的key
to// resource 中的id
);
// 绑定
listview.setAdapter(adapter);
}
}
]