Android ListView Multiple Columns from an Array
I have a list view with following row template.
<?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" andro开发者_JAVA百科id: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>
And the data is stored in a List as...
List<String[]> rows = new ArrayList<String[]>();
rows.add(new String[] { "id", "name" });
I want to know what Adapter I should use to fill the ListView with these rows assuming I need 2 column data in a single row?
Just use a SimpleAdapter with a list of HashMap
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)
You could implement the ListViewAdapter interface in a class and inflate the row-xml in the getView-method. You can use a regular activity with a simple LinearLayout and a list view in it.
精彩评论