您的位置:首页 > 产品设计 > UI/UE

Android UI设计——ListView控件与SimpleAdapter适配器(三)

2015-08-25 17:39 531 查看

SimpleAdapter

  SimpleAdapter也是Android自己提供的一个Adapter适配器,它与ArrayAdapter不同的是ArrayAdapter需要使用Android自己定义的view布局文件,而SimpleAdapter则可以使用我们自己定义的布局文件。要学习SimpleAdapter的使用首先然我们看一下SimpleAdapter的构造器:

  



  从图片中我们可以看出,SimpleAdapter只有一个构造器:

第一个参数
Context context
是指当前的Activity,我们传入this即可。

第二个参数
List<? extends Map<String, ?>>
是指传入的数据类型必须是List集合,集合存放的数据类型必须是Map。

第三个参数
int resource
是指View的布局文件。也就是用来显示数据的View。

第四个参数
String[] from
数据是以Map类型存放在List集合中的,from参数是指存放在List中每条Map数据的键值集合。

第五个参数
int[] to
是指将每条Map类型的数据中的不同键值对应到不同的得布局控件中。

  介绍完构造器的参数,大家可能还是不太懂到底是应该如何使用,没关系,我们通过例子来说明使用。

SimpleAdapter的使用

One.定义一个ListView的布局文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listview_array"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#f00000"
        android:dividerHeight="2dp">

    </ListView>

</LinearLayout>


Two.书写一个View的布局文件,将数据以该View的形式存放在ListView中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:gravity="center|left"
    android:background="@drawable/item_background">

    <ImageView
        android:id="@+id/image_photo"
        android:layout_width="70dp"
        android:layout_height="70dp" />
    <TextView
        android:id="@+id/textview_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名"
        android:textStyle="bold"
        android:textColor="#0e99ff"
        android:textSize="20sp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp">

        <TextView
            android:id="@+id/textview_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别"
            android:textStyle="bold"
            android:textColor="#009900"
            android:textSize="15sp"/>
        <TextView
            android:id="@+id/textview_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄"
            android:textStyle="bold"
            android:textColor="#ff99ff"
            android:textSize="15sp" />
    </LinearLayout>
    <TextView
        android:id="@+id/textview_hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="爱好"
        android:textStyle="bold"
        android:textColor="#55eedd"
        android:textSize="20sp" />
</LinearLayout>


Three.创建数据。创建List的集合存放Map类型的数据,并对其进行初始化。(这些直接在Activity中定义)

public HashMap<String, String>  createHashMap(String name, String age, String sex, String hobby){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("age", age);
        map.put("sex",sex);
        map.put("hobby", hobby);
        return map;
    }
    public void initList(){
        mData = new ArrayList<HashMap<String, String>>();
        HashMap zhangsan =  createHashMap("张三", "18", "男", "喜欢打篮球,睡觉");
        mData.add(zhangsan);
        HashMap lisi =  createHashMap("李四", "19", "女", "喜欢看书");
        mData.add(lisi);
        HashMap wangwu =  createHashMap("王五", "22", "男", "喜欢玩游戏");
        mData.add(wangwu);
        HashMap zhaoliu =  createHashMap("赵六", "21", "男", "喜欢吃饭,睡觉");
        mData.add(zhaoliu);

    }


Four.在onCreate()方法中初始化数据,创建SimpleAdapter的对象

String[] from = new String[]{"name", "age", "sex", "hobby"};
    int[] to = new int[]{R.id.textview_name,R.id.textview_age,R.id.textview_sex,R.id.textview_hobby};
    initList();
        SimpleAdapter  simpleAdapter = new SimpleAdapter(this,mData,R.layout.item_simpleadapter,from,to);


Five.调用setAdapter()方法,将View添加到ListView中。

mListViewArray.setAdapter(simpleAdapter);


Activity整体的代码为:

public class ListActivity extends Activity {
private ListView mListViewArray;
//定义数据。
private List<HashMap<String, String >> mData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
//创建ListView的对象。
mListViewArray = (ListView) findViewById(R.id.listview_array);

//初始化数据,通过调用自己定义的初始化方法。
initList();
//创建SimpleAdapter对象,将添加到数据添加到自己定义的View中。
SimpleAdapter simpleAdapter = new SimpleAdapter(this,mData,R.layout.item_simpleadapter,
new String[]{"name", "age", "sex", "hobby"},new int[]{R.id.textview_name,R.id.textview_age,R.id.textview_sex,R.id.textview_hobby});
//将View添加到ListView中。
mListViewArray.setAdapter(simpleAdapter);
}
//定义Map类型的数据。
public HashMap<String, String> createHashMap(String name, String age, String sex, String hobby){
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("age", age);
map.put("sex",sex);
map.put("hobby", hobby);
return map;
}
//对数据进行初始化的方法。
public void initList(){
mData = new ArrayList<HashMap<String, String>>();
HashMap zhangsan = createHashMap("张三", "18", "男", "喜欢打篮球,睡觉");
mData.add(zhangsan);
HashMap lisi = createHashMap("李四", "19", "女", "喜欢看书");
mData.add(lisi);
HashMap wangwu = createHashMap("王五", "22", "男", "喜欢玩游戏");
mData.add(wangwu);
HashMap zhaoliu = createHashMap("赵六", "21", "男", "喜欢吃饭,睡觉");
mData.add(zhaoliu);
}
}


结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: