我有一个包含以下行模板的列表视图。
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout android:clickable="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/android_btn_large" android:gravity="center_vertical"
android:layout_weight="1">
<TextView android:id="@+id/txtLeftButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/imgLeftButton"
android:text="Riverside Park" android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"></TextView>
<ImageView android:id="@id/imgLeftButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="@id/txtLeftButton"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="@drawable/plus_icon_480">
</ImageView>
</RelativeLayout>
<RelativeLayout android:clickable="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/android_btn_large" android:gravity="center_vertical"
android:layout_weight="1">
<TextView android:id="@+id/txtRightButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/imgRightButton"
android:text="Riverside Park" android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"></TextView>
<ImageView android:id="@id/imgRightButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="@id/txtRightButton"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="@drawable/plus_icon_480">
</ImageView>
</RelativeLayout>
</LinearLayout>
数据存储在列表中......
List<String[]> rows = new ArrayList<String[]>();
rows.add(new String[] { "id", "name" });
我想知道我应该用什么适配器来填充这些行的ListView,假设我需要在一行中有2列数据?
答案 0 :(得分:1)
只需使用带有HashMap列表的SimpleAdapter
ListView list = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("name", "bob");
mylist.add(map);
//Each row is a new hashmap
map = new HashMap<String, String>();
map.put("id", "2");
map.put("name", "sally");
mylist.add(map);
//ect...
mSchedule = new SimpleAdapter(this, mylist, R.layout.listrow,
new String[] {"id", "name"}, new int[] {R.id.txtID, R.id.txtName});
list.setAdapter(mSchedule)
答案 1 :(得分:0)
您可以在类中实现ListViewAdapter接口,并在getView方法中扩展row-xml。您可以使用带有简单LinearLayout和列表视图的常规活动。