android ListView之——SimpleAdapter使用
来源:互联网 发布:网络依赖症的发展过程 编辑:程序博客网 时间:2024/06/05 11:09
上一篇我们讲到了最基本的ArrayAdapter,这次来说一下SimpleAdapter,同样作为适配器,它们有什么区别呢?
与ArrayAdapter中适配的数据列表List< String>不同,SimpleAdapter使用List< Map>数据源。由于Map可以包含各种不同的控件资源,比如:图片、文本、按钮的组合等,所以SimpleAdapter可以为包含多个控件的Item填充数据。其中,List中的一个Map对应一个Item,Map中的一个元素对应Item中的一个控件资源,这样,就可以把SimpleAdapter中的数据填充到ListView中。
来实现如下的界面
效果图:
下面看代码:
SimpleAdapter_Aty.java
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.ListView;import android.widget.SimpleAdapter;import com.example.practice.R;public class SimpleAdapter_Aty extends Activity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview_aty); listView = (ListView) findViewById(R.id.listView); /* * 这就是上述的List<Map>; API中给出的是List<? extends Map<String,Object>>; * 所以说,其中Map要是Map<String,Object>它的子类; 需要注意的是:它的Key值是String类型, * 它的Value是Object类型,我们可以想一下,由于这个map里面存放的是控件资源,所以Object类型是很有必要的。 */ List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); HashMap<String, Object> map1 = new HashMap<String, Object>(); map1.put("img", android.R.drawable.ic_menu_add); map1.put("id", "01"); map1.put("name", "mary"); HashMap<String, Object> map2 = new HashMap<String, Object>(); map2.put("img", android.R.drawable.ic_menu_always_landscape_portrait); map2.put("id", "02"); map2.put("name", "lili"); HashMap<String, Object> map3 = new HashMap<String, Object>(); map3.put("img", android.R.drawable.ic_menu_camera); map3.put("id", "03"); map3.put("name", "tom"); HashMap<String, Object> map4 = new HashMap<String, Object>(); map4.put("img", android.R.drawable.ic_menu_crop); map4.put("id", "04"); map4.put("name", "jack"); HashMap<String, Object> map5 = new HashMap<String, Object>(); map5.put("img", android.R.drawable.ic_menu_directions); map5.put("id", "05"); map5.put("name", "bob"); data.add(map1); data.add(map2); data.add(map3); data.add(map4); data.add(map5); /* * String[] from和int[] to是将Map里的数据和Item对应起来的关键; String[] * from里面存放的是Map的Key,int[] to里面存放的是Item的资源Id。 * 什么意思呢,也就是把from里的资源,给to相对应的Id,它们是一一对应的,这里一定不能弄错了。 */ String[] from = { "img", "id", "name" }; int[] to = { R.id.simpleAdapter_iv_icon, R.id.simpleAdapter_iv_id, R.id.simpleAdapter_tv_name }; SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.listview_item2, from, to); listView.setAdapter(adapter); }}
listview_aty.xml
<?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="vertical" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout>
listview_item2.xml
<?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" > <ImageView android:id="@+id/simpleAdapter_iv_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="32dp" /> <TextView android:id="@+id/simpleAdapter_iv_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" /> <TextView android:id="@+id/simpleAdapter_tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" /></LinearLayout>
以上就是BaseAdapter应用的一个简单小例子。
0 0
- android ListView之——SimpleAdapter使用
- Android杂谈--ListView之SimpleAdapter的使用
- Android之ListView与SimpleAdapter的使用
- Android杂谈--ListView之SimpleAdapter的使用
- Android杂谈--ListView之SimpleAdapter的使用
- Android--ListView之SimpleAdapter的使用(转)
- Android杂谈--ListView之SimpleAdapter的使用
- Android杂谈--ListView之SimpleAdapter的使用
- Android中ListView之SimpleAdapter的使用
- android组件ListView之SimpleAdapter简单使用
- Android ListView使用SimpleAdapter
- android——ListView功能的实现(使用simpleAdapter)
- [ListView]Android杂谈--ListView之SimpleAdapter的使用
- Android之SimpleAdapter和ListView
- ListView之SimpleAdapter的使用
- ListView之SimpleAdapter的使用
- ListView之SimpleAdapter的使用
- ListView之SimpleAdapter的使用 .
- 线性回归、梯度下降 - Andrew Ng机器学习公开课笔记1.1
- 损失函数 - Andrew Ng机器学习公开课笔记1.2
- 局部加权回归、欠拟合、过拟合 - Andrew Ng机器学习公开课笔记1.3
- 广义线性模型 - Andrew Ng机器学习公开课笔记1.6
- ubuntu运行自由门
- android ListView之——SimpleAdapter使用
- 创业者需要干掉的三种思维
- C语言各种关键字
- 函数指针问题,求解答
- PS讲6-----选区的布尔运算
- 开启红米2手机AGPS辅助,让红米2定位更快
- Java泛型编程使用总结
- C语言中的字符串
- 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。