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);    }}

结果如下:
Android UI设计——ListView控件与SimpleAdapter适配器(三)_第1张图片

更多相关文章

  1. 编写自定义的 Android Preference 组件
  2. 在 Android 应用程序中使用 Internet 数据
  3. Android Native代码中的status_t定义
  4. 自定义ListView中的分割线(转)
  5. Android 自定义实现TextView单行超出部分显示为省略号
  6. android中使用sqlite、复制assets下的数据库到SD卡、支持大于1M
  7. Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么
  8. 2015Android设备、系统、分辨率最新统计数据
  9. Android的参数大致分成两块:系统服务参数和平台系统信息。

随机推荐

  1. spark学习-SparkSQL--07-SparkContext类
  2. 如何通过使用where子句与字符串格式(varch
  3. 关于mysql的sql_mode的问题
  4. 【Mysql】实现中位数计算
  5. 约束数据库表,以便只有一行可以在列中具有
  6. sqlite developer过期解决办法
  7. python pandas库的应用(类比mysql语言)
  8. SQL Server 高可用性(一)AlwaysOn 技术
  9. 为什么我使用此Linq to Sql方法获得Inval
  10. PB中动态SQL处理BLOB的方式