1、SimpleAdapter是ArrayList和ListView的桥梁,这个ArrayList礼拜的每一项都是一个Map<String,?>类型。ArrayList当中的每一项Map对象都和ListView里边的每一项进行数据绑定一一对应。

2、SimpleAdapter的构造函数:

SimpleAdapter(Context  context, List<? extends Map<String, ?>>  data, int resource, String[]  from, int[] to)
参数:
1,context:上下文。
2,data:基于Map的list。Data里边的每一项都和ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
3,resource :就是一个布局layout,可引用系统提供的,也可以自定义。
4,from:这是个名字数组,每个名字是为了在ArrayList数组的每一个item索引Map<String,Object>的Object用的。
5,to:里面是一个TextView数组。这些TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。

 

一个例子:

activity_main.xml

<LinearLayout 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" >

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="姓名"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="年龄"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

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

</LinearLayout>

 

 

mylist.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="horizontal" >

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/age"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />
    

</LinearLayout>

 

MainActivity.java

 

package com.example.listviewooo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    private ListView listView;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        //构造函数  SimpleAdapter(Context  context, List<? extends Map<String, ?>>  data, int resource, String[]  from, int[] to)
        SimpleAdapter adapter = new SimpleAdapter(this, //构造函数里的Context,上下文
                getData(),//ListView里要显示的数据
                R.layout.mylist, //ListView的布局ID,可以用系统自带的也可以自定义
                new String[]{"name", "age"}, //from  与to一一对应的,在mylist中每显示一个view的key
                new int[]{R.id.name, R.id.age});//to 与from对应,数值应该是自定义ListView里的每个view的Id
        listView = (ListView) findViewById(R.id.listview);
        //都是setAdapter
        listView.setAdapter(adapter);

    }

    public List<Map<String, Object>> getData()

    {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        for (int i = 1; i <= 20; i++)
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", "张三" + i);
            map.put("age", 20 + i);
            list.add(map);

        }

        return list;
    }

}
posted on 2015-06-07 10:28  QQ28902581  阅读(141)  评论(0编辑  收藏  举报