Android数据存储操作④Adapter之ArrayAdapter、SimpleAdapter

Adapter在Android中占据一个重要的角色,它是数据和UI(View)之间一个重要的纽带。在常见的View(ListView GridView Gallery Spinner)等地方都需要用到Adapter。

一、ArrayAdapter:

BaseAdapter的具体实现,在实例化时可以使用泛型结构,ArrayAdapter 负责把一个字符串数组中的数据填充到一个View当中。

1 // 首先声明一个Spinner类的对象
2   Spinner s = (Spinner)findViewById(R.id.flipper);
3   // 之后调用ArrayAdapter
4   ArrayAdapter < String > adapter = new ArrayAdapter < String > ( this ,android.R.layout.simple_spinner_item,mStrings);
5 android.widget.ArrayAdapter.ArrayAdapter(Context context, int textViewResourceId, String[] objects)
6   /*
7 *public ArrayAdapter(Context context, int textViewResourceId, T[] objects)
8 *Constructor
9 *Parameters
10 *context 当前context
11 *textViewResourceId 当初始化views时,包含一个文本视图的文件所对应的资源ID值。The resource ID for a layout file containing a *TextView to use when instantiating views.
12 *objects 在列表视图中所代表的对象。The objects to represent in the ListView.
13   */
14 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
15 s.setAdapter(adapter);
16   /*
17 *void android.widget.ArrayAdapter.setDropDownViewResource(int resource)
18 *public void setDropDownViewResource(int resource)
19 *设置布局资源来设置下拉视图。
20 *Sets the layout resource to create the drop down views.
21 *Parameters
22 *resource 定义下拉资源的布局资源the layout resource defining the drop down views
23 *See Also
24 * getDropDownView(int, android.view.View, android.view.ViewGroup)
25   */
26
27 s.setOnItemSelectedListener( this );
28
29   /*
30 *void android.widget.AdapterView.setOnItemSelectedListener(OnItemSelectedListener listener)
31 *public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)
32 *注册一个回调函数用来在当一个选项在此适配器视图中被选中时执行
33 *Register a callback to be invoked when an item in this AdapterView has been selected.
34 *Parameters
35 *listener 将要执行的响应函数The callback that will run
36   */

二、SimpleAdapter:

BaseAdapter的具体实现,SimpleAdapter 可以使用 List<? extends Map<String, ?>> 类型的数据填充到View当中。

构造函数:public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to),具体含义如下:

1、Context

2、List结构的数据源,其内部结构继承自Map

3、视图布局文件ID

4、Map中的Key,根据Key来得到对应的Value 

5、Map的Key得到的Value所对应的视图(TextView)

示例代码如下:

创建布局文件personitem.xml,代表ID、姓名、年龄,即每一行的三个元素:

1 <? xml version="1.0" encoding="utf-8" ?>
2   < RelativeLayout
3   xmlns:android ="http://schemas.android.com/apk/res/android"
4 android:layout_width ="fill_parent"
5 android:layout_height ="wrap_content" >
6 < TextView
7 android:layout_width ="60px"
8 android:layout_height ="wrap_content"
9 android:id ="@+id/personid"
10 >
11 </ TextView >
12 < TextView
13 android:layout_width ="160px"
14 android:layout_height ="wrap_content"
15 android:layout_toRightOf ="@id/personid "
16 android:layout_alignTop ="@id/personid "
17 android:gravity ="center_horizontal"
18 android:id ="@+id/name"
19 >
20 </ TextView >
21 < TextView
22 android:layout_width ="wrap_content"
23 android:layout_height ="wrap_content"
24 android:layout_toRightOf ="@id/name"
25 android:layout_alignTop ="@id/name"
26 android:id ="@+id/age"
27 >
28 </ TextView >
29 </ RelativeLayout >


编写视图主界面main.xml,加入一个ListView:

1 <? xml version="1.0" encoding="utf-8" ?>
2 < LinearLayout
3 xmlns:android ="http://schemas.android.com/apk/res/android"
4 android:orientation ="vertical"
5 android:layout_width ="fill_parent"
6 android:layout_height ="fill_parent"
7 >
8 < ListView
9 android:id ="@+id/personList"
10 android:layout_width ="fill_parent"
11 android:layout_height ="wrap_content"
12 ></ ListView >
13 </ LinearLayout >

将ListView绑定SimpleAdapter:

1 public class PersonActivity extends Activity {
2 /** Called when the activity is first created. */
3 private final static String TAG = " PersonActivity " ;
4 private ListView listView;
5 private PersonService personService;
6 @Override
7 public void onCreate(Bundle savedInstanceState) {
8 super .onCreate(savedInstanceState);
9 setContentView(R.layout.main);
10 istView = (ListView)findViewById(R.id.personList);
11 List < HashMap < String, String >> data = new ArrayList < HashMap < String,String >> ();
12 HashMap < String, String > title = new HashMap < String, String > ();
13 title.put( " personid " , " 编号 " );
14 title.put( " name " , " 姓名 " );
15 title.put( " age " , " 年龄 " );
16 data.add(title);
17 SimpleAdapter adapter = new SimpleAdapter(PersonActivity. this ,
18 data, R.layout.personitem, new String[]{ " personid " , " name " , " age " },
19 new int []{R.id.personid, R.id.name, R.id.age});
20 listView.setAdapter(adapter);
21 }
22 }


你可能感兴趣的:(SimpleAdapter)