在Android中ListView 2行和每个项目的图标

Raz*_*van 3 android android-listview

Hy,我想在ListView的一个项目中添加一个图标和2行.首先是价值,第二是价值2

String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
String[] values2 = new String[] `enter code here`{ "Android2", "iPhone2",        "WindowsMobile2",
            "Blackberry2", "WebOS2", "Ubuntu2", "Windows72", "Max OS  X2",
            "Linux2", "OS/22" };
Run Code Online (Sandbox Code Playgroud)

我的列表xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我每行的xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="22px"
    android:layout_height="22px"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px"
    android:layout_marginTop="4px"
    android:src="@drawable/ic_launcher" >
</ImageView>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="20px" />

<TextView
    android:id="@+id/label2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的代码是:

 public class listaNoua extends ListActivity {
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
    String[] values2 = new String[] { "Android2", "iPhone2", "WindowsMobile2",
            "Blackberry2", "WebOS2", "Ubuntu2", "Windows72", "Max OS X2",
            "Linux2", "OS/22" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>  (this,R.layout.rowlayout, R.id.label2, values2);
    setListAdapter(adapter);

}
Run Code Online (Sandbox Code Playgroud)

如果我添加:

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout,   R.id.label2, values2 , R.id.label, values);
Run Code Online (Sandbox Code Playgroud)

代码不起作用

有谁能够帮我 ?

Ars*_*war 5

您应该使用列表自定义适配器.你可以在这里找到一些好的例子

http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/

我想你在行XML上忘记了LinearLayout的方向.问题是默认情况下线性布局具有"水平"方向.该行应如下所示,以显示列表中的所有3个项目.

<ImageView
    android:id="@+id/icon"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="4dp"
    android:src="@drawable/ic_launcher" >
</ImageView>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/label2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


bal*_*azs 5

您可以使用SimpleAdapter.

    List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
    data.add(createRow("Android", "Android2"));
    data.add(createRow("iPhone", "iPhone"));

    String[] from = {"value1", "value2"};
    int[] to = {R.id.label, R.id.label2};

    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_layout, from, to);
Run Code Online (Sandbox Code Playgroud)

以及创建行的功能

private Map<String, ?> createRow(String value1, String value2) {
    Map<String, String> row = new HashMap<String, String>();
    row.put("value1", value1);
    row.put("value2", value2);
    return row;
}
Run Code Online (Sandbox Code Playgroud)