Android listview,想在listview的每一行都有不同的图片

时间:2011-03-07 00:47:38

标签: android listview

我正在尝试为每个列出的项目创建一个具有不同图像和文本的列表框。我尝试创建两个setListAdapter,一个用于显示文本字符串,另一个用于图像名称。它不起作用,我假设有另一种方法可以做到这一点。

我的名单上的CTICTY CLASS

public class cFashions extends ListActivity  {
    TextView selection;
    // list to fill list box wi*/
/*
    String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR",
            "JEWELRY","ACCESSORIES"};
*/
    String[] items={"CASUAL"};
    String[] icons={"icon"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fashions);

        setListAdapter(new ArrayAdapter<String>(this,
            R.layout.row, R.id.label,
            items));

        setListAdapter(new ArrayAdapter<String>(this,
            R.layout.row, R.id.icon,
            icons));
            selection=(TextView)findViewById(R.id.selection);
   }
}

列表中每个单元格的XML文件

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

<ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:paddingLeft="2px"
        android:paddingRight="2px"
        android:paddingTop="2px"
        android:layout_height="wrap_content"

    />

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

泰德

1 个答案:

答案 0 :(得分:5)

您需要使用一个适配器,而不是两个,适配器知道如何处理TextViewImageViewHere is a free excerpt从我的一本书中详细介绍了这个主题。