标签:
1.定义
数据适配器是AdapterView视图(如ListView - 列表视图控件、Gallery - 缩略图浏览器控件、GridView - 网格控件、Spinner - 下拉列表控件、AutoCompleteTextView - 自动提示文本框、ExpandableListView - 支持展开/收缩功能的列表控件等)与数据之间的桥梁,用来处理数据并将数据绑定到AdapterView上。
2.作用
数据适配器是连接数据源和视图界面的桥梁。
3.分类
包括ArrayAdapter(数组适配器)以及SimpleAdapter(简单适配器)。其中:
4.实现
5.案例
(1)具体用到的类:
(2)代码实例:
1 ListView listView; 2 ArrayAdapter arrayAdapter; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_violet); 8 listView = (ListView) findViewById(R.id.listView); 9 String[] data={"苹果","香蕉","桃子","西瓜"}; 10 arrayAdapter=new ArrayAdapter(this,R.layout.abc_simple_dropdown_hint,data);//上下文,当前ListView加载的每一个列表所对应的布局文件,数据源)
11 listView.setAdapter(arrayAdapter);
12 }
1 ListView listView; 2 SimpleAdapter simpleAdapter; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_violet); 8 listView = (ListView) findViewById(R.id.listView); 9 List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); 10 String[] persons = {"张老师", "陈老师", "李老师", "黄老师"}; 11 String[] phoneNums = {"18706218201", "18706214521", "13908761231", "18512390812"}; 12 for (int i = 0; i < persons.length; i++) { 13 HashMap<String, Object> map = new HashMap<String, Object>(); 14 map.put("username", persons[i]); 15 map.put("phoneNum", phoneNums[i]); 16 //插入图片 17 map.put("photo",R.drawable.cc); 18 data.add(map); 19 }
20 //(context,data,resource,from,to) 21 simpleAdapter = new SimpleAdapter(this, data, R.layout.list_item, new String[]{"username","photo", "phoneNum"}, new int[]{R.id.username,R.id.photo, R.id.phoneNum});
22 listView.setAdapter(simpleAdapter); 23 }
标签:
原文地址:http://www.cnblogs.com/TechSnail/p/4678945.html