Adapter

来源:互联网 发布:linux cpu调为高性能 编辑:程序博客网 时间:2024/06/06 23:14

Adapter

官方文档是这样介绍adapter的:

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

adapter是AdapterView和data之间的桥梁,也就是数据和UI(View)之间一个重要的纽带。adapter是接口,其层级关系如下:

这里写图片描述

ArrayAdapter

A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

ArrayAdapter是继承BaseAdapter的实体类,其构造函数

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

中,我们将objects中的数据显示在textViewResourceId指示的布局中。

例1:

package com.example.z1178.test;import android.app.ListActivity;import android.os.Bundle;import android.util.Log;import android.widget.ArrayAdapter;public class MainActivity extends ListActivity {    private static final String TAG="debug";    @Override    protected void onCreate(Bundle savedInstanceState) {        Log.d(TAG, "onCreate");        super.onCreate(savedInstanceState);        String [] strs={"siege","cage","jack","tom","lilei"};        ArrayAdapter<String> adapter= new ArrayAdapter<String>                (this,android.R.layout.simple_expandable_list_item_1,strs);        setListAdapter(adapter);    }}

其效果如图:

这里写图片描述

例2. 或者我们这样做:

package com.example.z1178.test;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.ArrayAdapter;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends Activity {    private static final String TAG="debug";    private ListView listView;    @Override    protected void onCreate(Bundle savedInstanceState) {        Log.d(TAG, "onCreate");        super.onCreate(savedInstanceState);        listView=new ListView(this);        listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,getData()));        setContentView(listView);    }    private List<String> getData() {        List<String> dataList=new ArrayList<String>();        dataList.add("Lee Norris");        dataList.add("Cassie Scerbo");        dataList.add("Rami Malek");        dataList.add("Corey Stoll");        dataList.add("Cara Delevingne");        return dataList;    }}

效果如图:

这里写图片描述

例1和例2我们分别使用了不同的ArrayAdapter构造器,将数据映射到ListView上。

SimpleAdapter

官方关于其说明是这样的:

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) or setViewImage(ImageView, String) is invoked.

If no appropriate binding can be found, an IllegalStateException is thrown.

例3:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >  <ImageView      android:id="@+id/img"      android:layout_width="50dp"      android:layout_height="50dp"      android:layout_margin="5dp"/>  <TextView      android:id="@+id/title"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textColor="#000000"      android:textSize="20sp"/></LinearLayout>

package com.example.z1178.test;import android.app.ListActivity;import android.os.Bundle;import android.util.Log;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends ListActivity {    private static final String TAG="debug";    private ListView listView;    @Override    protected void onCreate(Bundle savedInstanceState) {        Log.d(TAG, "onCreate");        super.onCreate(savedInstanceState);        SimpleAdapter adapter=new SimpleAdapter(this,getData(),                R.layout.activity_main,new String[]{"title","img"},new int[]{R.id.title,R.id.img});        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","Don't Cry");        map.put("img",R.drawable.layout_image);        list.add(map);        map=new HashMap<String,Object>();        map.put("title","Counting Star");        map.put("img", R.drawable.layout_image);        list.add(map);        map=new HashMap<String,Object>();        map.put("title","Let It Be");        map.put("img", R.drawable.layout_image);        list.add(map);        return list;    }}

效果如图:

这里写图片描述

SimpleCursorAdapter

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. Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked. If no appropriate binding can be found, an IllegalStateException is thrown. If this adapter is used with filtering, for instance in an AutoCompleteTextView, you can use the SimpleCursorAdapter.CursorToStringConverter and the FilterQueryProvider interfaces to get control over the filtering process. You can refer to convertToString(android.database.Cursor) and runQueryOnBackgroundThread(CharSequence) for more information.

1 0
原创粉丝点击