图文列表,关于Simpleadapter

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

su.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="60dp"

    android:orientation="horizontal">
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="8dp"

        />
    <LinearLayout
        android:layout_toRightOf="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="60dp"

        android:orientation="vertical">
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="20dp"


            android:textSize="16sp"
            />
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"


            android:textSize="10sp"/>

    </LinearLayout>

</LinearLayout>

MainActivity.java:

public class MainActivity extends Activity {
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.listview);
        SimpleAdapter adapter = new SimpleAdapter(this,getData(),
                R.layout.su,
                new String[] {"img","title","info"},
                new int[] {R.id.img,R.id.title,R.id.info});
        lv.setAdapter(adapter);
    }

    private List<Map<String,Object>> getData(){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1802);
        map.put("title","Smith");
        map.put("info", "电台DJ");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1804);
        map.put("title","西施");
        map.put("info", "四大美女之一");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1805);
        map.put("title","大圣归来");
        map.put("info", "国产电影");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1809);
        map.put("title","旗木卡卡西");
        map.put("info", "copy忍者");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1810);
        map.put("title","飞雷神之术");
        map.put("info", "来无影去无踪");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1811);
        map.put("title","HelloKitty");
        map.put("info", "女生的最爱");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1816);
        map.put("title","机械暴龙兽");
        map.put("info", "亚古兽的完全体");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("img",R.drawable.s1811);
        map.put("title","HelloKitty");
        map.put("info", "女生的最爱");
        list.add(map);

        return list;
    }
}
原文地址:https://www.cnblogs.com/abelsu/p/4661459.html