您的位置:首页 > 移动开发 > Android开发

Android常见控件之SimpleAdapter和List

2013-08-26 19:19 507 查看
一、SimpleAdapter

SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义 好的视图。你可以指定数据支持的列表如ArrayList组成的Map。在ArrayList中 的每个条目对应List中的一行。Maps包含每行数据。你可以指定一个定义了被用 于显示行的视图XML文件,通过关键字映射到指定的视图。

构造函数

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

参数

context 关联SimpleAdapter运行着的视图的上下文。

data 一个Map的列表。在列表中的每个条目对应列表中的一行,应该包含所 有在from中指定的条目

resource 一个定义列表项目的视图布局的资源唯一标识。布局文件将至少应 包含哪些在to中定义了的名称。

from 一个将被添加到Map上关联每一个项目的列名称的列表

to 应该在参数from显示列的视图。这些应该全是TextView。在列表中最初的 N视图是从参数from中最初的N列获取的值。

一个SimlpleAdapter是这样工作的。假设将SimpleAdapter用于ListView。那 么ListView的每一个列表项就是 resource参数值指定的布局。而data参数就是 要加载到ListView中的数据。我们先看每一个列表项,假设列表项所对应的布局 文件中包含了两个组件:TextView和EditText,id分别为textview和edittext。 那么在加载列表项时,需要通过组件的id和data参数中 List元素中的Map对象对 应。因此,from参数Map对象的key,而to表示组件的id,例如,本例中的参数值
为from = new String[]{"userId", "userName"},to = new int[]{R.id.userId,R.id.userName}。意思就是将Map对象中key为userId 的value绑定到 R.id.userId上,userName也类似。现在来看data参数,一个 ListView由多个列表项组成。每一个列表项由一个Map对象提供数据,而多个列 表项则由List对象提供多个 Map对象。

二、ListView

在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容 ,并且能够根据数据的长度自适应显示。

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据 具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和 SimpleCursorAdapter。其中以ArrayAdapter最为简单,只能展示一行字。 SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter 可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以 列表的形式展示出来。SimpleAdapter继承自AdapterView。我们可以通过 setOnItemClickListener()方法给ListView添加监听器,当用户点击某一个列表
项中执行相应的操作。在监听器中需要复写public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)方法。

如果需要访问与被选项相关的数据,执行程序可以调用getItemAtPosition (position)。

参数

parent  发生点击动作的AdapterView。

view 在AdapterView中被点击的视图(它是由adapter提供的一个视图)。

position 视图在adapter中的位置。

id 被点击元素的行id。

示例:我们一SimpleAdapter为例说明ListView的用法。

Android_ListView.java
01.package com.idea.org;
02.
03.import java.util.ArrayList;
04.import java.util.HashMap;
05.
06.import android.app.Activity;
07.import android.os.Bundle;
08.import android.view.View;
09.import android.widget.AdapterView;
10.import android.widget.AdapterView.OnItemClickListener;
11.import android.widget.ListView;
12.import android.widget.SimpleAdapter;
13.import android.widget.Toast;
14.
15.public class Android_ListView extends Activity {
16.    /** Called when the activity is first created. */
17.    @Override
18.    public void onCreate(Bundle savedInstanceState) {
19.        super.onCreate(savedInstanceState);
20.        setContentView(R.layout.main);
21.        ArrayList<HashMap<String,String>>list =new ArrayList<HashMap<String,String>>();
22.        HashMap<String,String> map1=new HashMap<String,String>();
23.        HashMap<String,String> map2=new HashMap<String,String>();
24.        HashMap<String,String> map3=new HashMap<String,String>();
25.        ListView listView=(ListView)findViewById(R.id.listView);
26.        map1.put("userId", "100001");
27.        map1.put("userName", "用户一");
28.        list.add(map1);
29.        map2.put("userId", "100002");
30.        map2.put("userName", "用户二");
31.        list.add(map2);
32.        map3.put("userId", "100003");
33.        map3.put("userName", "用户三");
34.        list.add(map3);
35.        //定义一个SimpleAdapter,每一个行有两个TextView,分别显示userId和userName
36.        SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.user,
37.                new String[]{"userId","userName"},new int[]{R.id.userId,R.id.userName});
38.        //为ListView添加适配器
39.        listView.setAdapter(simpleAdapter);//设置listView背后的数据为simpleAdapter。
40.        /*为listView添加单击监听器,需要import android.widget.AdapterView.OnItemClickListener;语句
41.         * 当点击某一个列表项时,用Toast显示这个列表项中的文字内容
42.         */
43.        listView.setOnItemClickListener(new OnItemClickListener() {
44.
45.            @Override
46.            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
47.              long arg3) {
48.             ListView listView=(ListView)arg0;
49.             Toast.makeText(Android_ListView.this, listView.getItemAtPosition(arg2).toString(),
50.                     Toast.LENGTH_SHORT).show();
51.            }
52.           });
53.    }   
54.}


main.xml
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.    android:orientation="vertical"
04.    android:layout_width="fill_parent"
05.    android:layout_height="wrap_content"
06.    >
07.    <!--添加一个ListView控件  -->
08.    <ListView
09.        android:id="@+id/listView"
10.        android:layout_width="fill_parent"
11.        android:layout_height="wrap_content"
12.        />
13.</LinearLayout>


SimpleAdapter所用的布局文件user.xml
01.<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.    android:orientation="horizontal"
04.    android:layout_width="fill_parent"
05.    android:layout_height="fill_parent"
06.    android:paddingRight="10dip"
07.    android:paddingLeft="10dip"
08.    android:paddingTop="1dip"
09.    android:paddingBottom="1dip"
10.    >
11.    <!--ListView的每一项都有两个TextView,分别显示userId和userName  -->
12.    <TextView
13.        android:id="@+id/userId"
14.        android:layout_width="fill_parent"
15.        android:layout_height="wrap_content"
16.        android:textSize="20pt"
17.        android:layout_weight="1"
18.        />
19.    <TextView
20.        android:id="@+id/userName"
21.        android:layout_width="fill_parent"
22.        android:layout_height="wrap_content"
23.        android:textSize="20pt"
24.        android:layout_weight="1"
25.        />
26.</LinearLayout>


运行效果





奇怪的是ListView无法充满Android3.0模拟器的整个屏幕,不知道是由于 Android3.0版本的问题,还是其它的什么原因。



来源:http://blog.csdn.net/ITCEOjingying/archive/2011/04/11/6315060.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: