ListView simpleAdapter的基本使用

使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键 值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个 SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。

---listviewitem.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:layout_marginTop="5dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        android:src="@drawable/p1" />

    

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.08"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="16dp" />

        <TextView
            android:id="@+id/t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
<Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

-----主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/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

-----java文件

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewtest);
        
        ListView lv=(ListView)findViewById(R.id.listView1);
        
        //{{ 形成list集合
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "G1d");
        map.put("info", "google 1");
        map.put("img", R.drawable.p1);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("title", "G2d");
        map.put("info", "google 2");
        map.put("img", R.drawable.p2);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("title", "G3d");
        map.put("info", "google 3");
        map.put("img", R.drawable.p3);
        list.add(map);
        //}}
        
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.listviewitem,
                new String[]{"title","info","img"},
                new int[]{R.id.t1,R.id.t2,R.id.img1});
        lv.setAdapter(adapter);
        
    }

posted on 2015-04-18 13:19  68tour  阅读(104)  评论(0编辑  收藏  举报

导航