简体   繁体   English

Android自定义列表SimpleAdapter-选择行图标

[英]Android custom List SimpleAdapter - chose row icon

Layout: 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/black">

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

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/icon"/>

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

        <TextView android:id="@+id/line_a"
            android:textSize="14dp"
            android:textColor="@android:color/white"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"/>

        <TextView android:id="@+id/line_b"
            android:textSize="10dp"
            android:textColor="@android:color/darker_gray"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

Java: Java:

    SimpleAdapter sa;
    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
    HashMap<String,String> item;

    for(int i = 0; i < j; i++){
        item = new HashMap<String,String>();
        item.put( "line1", ListData[i][0]);
        item.put( "line2", ListData[i][1]);
        item.put( "type", ListData[i][2]);
        list.add( item );
    }

    sa = new SimpleAdapter(this, list,
            R.layout.two_lines_list,
            new String[] { "line1","line2" },
            new int[] {R.id.line_a, R.id.line_b});
                         // can you add the setImageView here? If yes, how?
    setListAdapter( sa );
}

How can I make the icon to get an image from res/drawable... depending on the type (ListData[i][2] inserted in the HashMap)? 如何根据类型(在HashMap中插入的ListData [i] [2])使图标从res / drawable获取图像? eg. 例如。 if the type is "1" set icon1.png at the beginning of the 2-line-row 如果类型为“ 1”,则在2行行的开头设置icon1.png

You should override SimpleAdapter.getView(int position, View convertView, ViewGroup parent) method, and get the list item with the current position(the first param in that method), then check the value set set the icon you want. 您应该重写SimpleAdapter.getView(int position, View convertView, ViewGroup parent)方法,并获取具有当前位置的列表项(该方法的第一个参数),然后检查设置为所需图标的值集。

NOTE: you' d better use ViewHolder pattern in the getView method for performance. 注意:最好在getView方法中使用ViewHolder模式以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM