Android ListView从一个数组中查看多个列

Android ListView从一个数组中查看多个列,android,android-layout,Android,Android Layout,我有一个具有以下行模板的列表视图 <?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:o

我有一个具有以下行模板的列表视图

<?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" });
List rows=new ArrayList();
添加(新字符串[]{“id”,“name”});

假设一行中需要两列数据,我想知道我应该使用什么适配器来用这些行填充ListView?

您可以在类中实现ListViewAdapter接口,并在getView方法中膨胀行xml。您可以使用带有简单线性布局和列表视图的常规活动。

您可以在类中实现ListViewAdapter接口,并在getView方法中膨胀行xml。您可以使用带有简单线性布局和列表视图的常规活动。

只需使用带有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)
ListView列表=(ListView)findViewById(R.id.ListView01);
ArrayList mylist=新的ArrayList();
HashMap=newHashMap();
地图放置(“id”、“1”);
地图。放置(“名称”、“鲍勃”);
添加(地图);
//每一行都是一个新的hashmap
map=新的HashMap();
地图放置(“id”、“2”);
地图。放置(“姓名”、“萨利”);
添加(地图);
//等等。。。
mSchedule=new simpledapter(this,mylist,R.layout.listrow,
新字符串[]{“id”,“name”},新int[]{R.id.txtID,R.id.txtName});
list.setAdapter(msSchedule)

只需将SimpleAdapter与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)
ListView列表=(ListView)findViewById(R.id.ListView01);
ArrayList mylist=新的ArrayList();
HashMap=newHashMap();
地图放置(“id”、“1”);
地图。放置(“名称”、“鲍勃”);
添加(地图);
//每一行都是一个新的hashmap
map=新的HashMap();
地图放置(“id”、“2”);
地图。放置(“姓名”、“萨利”);
添加(地图);
//等等。。。
mSchedule=new simpledapter(this,mylist,R.layout.listrow,
新字符串[]{“id”,“name”},新int[]{R.id.txtID,R.id.txtName});
list.setAdapter(msSchedule)

我的意思是rows集合中的两行必须填写在一个ListView行中。假设我无法更改集合或行。我不确定我是否理解,我发布的方法放置了2行(或更多)单行中的数据列我的意思是rows集合中的两行必须在单个ListView行中填充。假设我无法更改集合或行。我不确定我是否理解,我发布的方法将两列(或更多)数据放在一行中