public class SimpleAdapterActivity extends Activity {
private SimpleAdapter sim_adapter;
private Listdatalist;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
lv=(ListView) findViewById(R.id.lv1);
//新建一个map键值对数据集合
datalist=new ArrayList();
//新建一个适配器 适配器加载数据源
sim_adapter=new SimpleAdapter(this, getData(), R.layout.simple, new String[]{"img1","tv1"} ,new int[]{R.id.img1,R.id.tv1});
//视图加载适配器
lv.setAdapter(sim_adapter);
}
//数据源
private List getData(){
Map map=new HashMap();//创建一个键值对对象
map.put("img1", R.drawable.img1);//添加键-值对
map.put("tv1", "可爱萝莉");
map.put("img2", R.drawable.img3);
map.put("tv2", "功夫兔");
map.put("img3", R.drawable.img3);
map.put("tv3", "小狐狸");
map.put("img4", R.drawable.img4);
map.put("tv4", "元气少女");
datalist.add(map);
return datalist;
}
} |