SimpleAdapter
Class Overview
An easy adapter to map static data to views defined in an XML file. You can specify the
data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to
one row in the list. The Maps contain the data for each row. You also specify an XML file
that defines the views used to display the row, and a mapping from keys in the Map to
specific views. Binding data to views occurs in two phases. First, if a SimpleAdapter.ViewBinder
is
available, setViewValue(android.view.View, Object, String)
is invoked. If the returned value is true, binding
has occurred. If the returned value is false, the following views are then tried in order:
- A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.
- TextView. The expected bind value is a string and
setViewText(TextView, String)
is invoked. - ImageView. The expected bind value is a resource id or a string and
setViewImage(ImageView, int)
orsetViewImage(ImageView, String)
is invoked.
If no appropriate binding can be found, an IllegalStateException
is thrown.
看翻译:
这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。你可以指定由Map组成的List(比如ArrayList)类型
的数据。在ArrayList中的每个条目对应List中的一行。Maps包含每一行的数据。你可以指定一个XML布局以指定每一行的视
图,根据Map中的数据映射关键字到指定的视图。绑定数据到视图分两个阶段,首先,如果设置
了SimpleAdapter.ViewBinder,那么这个设置的ViewBinder的setViewValue(android.view.View, Object,
String)将被调用。如果setViewValue的返回值是true,则表示绑定已经完成,将不再调用系统默认的绑定实现。如果返回值
为false,视图将按以下顺序绑定数据:
- 如果View实现了Checkable(例如CheckBox),期望绑定值是一个布尔类型。
- TextView.期望绑定值是一个字符串类型,通过调用setViewText(TextView, String)绑定。
- ImageView,期望绑定值是一个资源id或者一个字符串,通过调用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)绑定数据。
构造函数(Public Constructors):
SimpleAdapter( Context context, List<? extends Map< String, ?>> data, int resource, String[] from, int[] to)
参数
context SimpleAdapter关联的View的运行环境(关联SimpleAdapter运行着的视图的上下文)
data 一个Map组成的List。在列表中的每个条目对应列表中的一行,每一个map中应该包含所有在from参数中指定的键
resource 一个定义列表项的布局文件的资源ID。布局文件将至少应包含那些在to中定义了的ID
from 一个将被添加到Map映射上的键名
to 将绑定数据的视图的ID,跟from参数对应,这些应该全是TextView
一个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是比较常用的组件,它以列表的形式展示具体内容 ,并且能够根据数据的长度自适应显示。
列表的显示需要三个元素:
- ListVeiw 用来展示列表的View。
- 适配器 用来把数据映射到ListView上的中介。
- 数据 具体的将被映射的字符串,图片,或者基本组件。
一行字。 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。
package com.example.android_intentforresult;
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.widget.ListView;
import android.widget.SimpleAdapter;
public class SimpleAdapterTest extends Activity {
//对应xml文件中的textview中显示的文字;simpleAdapter构造方法中的参数from:一个将被添加到Map映射上的键名
private String[] textNames = new String[]{"sunshine","dream","today","tomorrow","yesterday"};
//对应xml文件中ImageView中的显示的图片;simpleAdapter构造方法中的参数t:将绑定数据的视图的ID,跟from参数对应,
private int[] imagesId = new int[]{R.drawable.tiger,R.drawable.leaf,R.drawable.libai,R.drawable.lijiang,R.drawable.qingzhao};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simpleadapter);
//指定页面上加载的ListView
ListView listView = (ListView)this.findViewById(R.id.mylist);
//创建一个List集合,list集合的元素是Map
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
for (int i = 0; i < textNames.length; i++) {
//创建一个map,每个map中存放listview列表中显示的图片和文字信息
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imagesId[i]);
map.put("text", textNames[i]);
list.add(map);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(
SimpleAdapterTest.this,
list,
R.layout.simpleadapter,
new String[]{"image","text"},
new int[]{R.id.imageId,R.id.textName});
listView.setAdapter(simpleAdapter);
}
}
布局文件:simpleadapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 定义一个List -->
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- 定义一个ImageView,用于作为列表项的一部分。 -->
<ImageView
android:contentDescription="@string/girl"
android:id="@+id/imageId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" />
<!-- 定义一个TextView,用于作为列表项的一部分。 -->
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textSize="16dp" />
</LinearLayout>

Demo2
package com.example.android_intentforresult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class SimpleAdapter1 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this,
getData(),
R.layout.vlist,
new String[] { "title", "info", "img" },
new int[] { R.id.stitle, R.id.sinfo, R.id.simg });
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "Tiger");
map.put("info", "I am 1");
map.put("img", R.drawable.tiger);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "Girl");
map.put("info", "I am girl");
map.put("img", R.drawable.qingzhao);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "Boy");
map.put("info", "I am boy");
map.put("img", R.drawable.libai);
list.add(map);
return list;
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#000000" >
<ImageView
android:id="@+id/simg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:contentDescription="@string/girl" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/stitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="22dp" />
<TextView
android:id="@+id/sinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>

SimpleCursorAdapter
sdk的解释是这样的:An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views。简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的 TextView中。下面的程序是从电话簿中把联系人显示到类表中。先在通讯录中添加一个联系人作为数据库的数据。然后获得一个指向数据库的Cursor并且定义一个布局文件(当然也可以使用系统自带的)。
package com.example.android_intentforresult;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
@SuppressWarnings("deprecation")
public class SimpleCursorAdapterTest extends Activity {
private ListView listView;
// private List<String> data = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = new ListView(this);
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null,
null, null, null);
startManagingCursor(cursor);
ListAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_1, cursor,
new String[] { People.NAME },
new int[] { android.R.id.text1 });
listView.setAdapter(listAdapter);
setContentView(listView);
}
}
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);//先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。
startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。
SimpleCursorAdapter 构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:一个包含数据库的列的String型数组,一个包含布局文件中对应组件id的int型数组。其作用是自动的将String型数组所表示的每一列数据映射到布局文件对应id的组件上。上面的代码,将NAME列的数据一次映射到布局文件的id为text1的组件上。
注意:需要在AndroidManifest.xml中如权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
运行后效果如下图: