ListView的使用----ArrayAdapter与SimpleAdapter

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		String[] objects = new String[]{
				"小志",
				"小志的儿子",
				"萌萌"
		};
		
		ListView lv = (ListView) findViewById(R.id.lv);
//		lv.setAdapter(new ArrayAdapter(this, R.layout.item_listview, R.id.tv_name, objects));
		
		//集合中每个元素都包含ListView条目需要的所有数据,该案例中每个条目需要一个字符串和一个整型,所以使用一个map来封装这两种数据
		List> data = new ArrayList>();
		
		Map map1 = new HashMap();
		map1.put("photo", R.drawable.photo1);
		map1.put("name", "小志的儿子");
		data.add(map1);
		
		Map map2 = new HashMap();
		map2.put("photo", R.drawable.photo2);
		map2.put("name", "小志");
		data.add(map2);
		
		Map map3 = new HashMap();
		map3.put("photo", R.drawable.photo3);
		map3.put("name", "赵帅哥");
		data.add(map3);
		
		lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview, 
				new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));
	}


}

你可能感兴趣的:(Android)