使用Android内部定义模板
例如:
使用自定义模板
例如:
PS:
SimpleAdapter参数解释
This为Context
List为数据
R.layout.activity2为对应的activity2.xml模板
new String[]{"姓名","年龄"}为list对应
R.id.view1为模板定义的TextView,显示顺序
Activity2.xml:
例如:
Activity的onCreate方法中:
String[] data = {"第一列","第二列","第三列"};
android.widget.ListView list = new android.widget.ListView(this);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
setContentView(list);
使用自定义模板
例如:
Activity的onCreate方法中:
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map = new HashMap<String,String>();
map.put("姓名", "小王");
map.put("年龄", "12");
list.add(map);
ListView view = new ListView(this);
view.setAdapter(new SimpleAdapter(this, list, R.layout.activity2, new String[]{"姓名","年龄"}, new int[]{R.id.view1,R.id.view2}));
setContentView(view);
PS:
SimpleAdapter参数解释
This为Context
List为数据
R.layout.activity2为对应的activity2.xml模板
new String[]{"姓名","年龄"}为list对应
R.id.view1为模板定义的TextView,显示顺序
Activity2.xml:
<TextView android:id="@+id/view1" android:layout_width="100px" android:layout_height="wrap_content"/>
<TextView android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>