需求:
一、UI设计,数据显示的格式;
1.把数据以单列的方式显示,使用ListView,例如微博条目,商品列表;
2.以多列的方式显示,用GridView,例如图片缩略图,功能表;
3.单列,以分类的方式显示,用ExpandableListView,例如QQ好友分组,通讯录好友分组,etc;
二、数据适配,数据的容器类型;
1.条目只是单个TextView,可以是用数组适配器ArrayAdapter<T>,可以传[]<T>,也可以传List<T>,传数组会被转成集合;
/**
* Constructor
*
* @param context The current context.
* @param resource The resource ID for a layout file containing a layout to use when
* instantiating views.
* @param textViewResourceId The id of the TextView within the layout resource to be populated
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
init(context, resource, textViewResourceId, objects);
}
//重载方式定义构造方法;
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
init(context, resource, textViewResourceId, Arrays.asList(objects));
}
private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
mContext = context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mResource = mDropDownResource = resource;
mObjects = objects;
mFieldId = textViewResourceId;
}
2.如果条目是有一对数据组成,那就不能用ArrayAdapter了,应该用SimpleAdapter,传人的是以Map为元素的List;
/**
* Constructor
*
* @param context The context where the View associated with this SimpleAdapter is running
* @param data A List of Maps. Each entry in the List corresponds to one row in the list. The
* Maps contain the data for each row, and should include all the entries specified in
* "from"
* @param resource Resource identifier of a view layout that defines the views for this list
* item. The layout file should include at least those named views defined in "to"
* @param from A list of column names that will be added to the Map associated with each
* item.
* @param to The views that should display column in the "from" parameter. These should all be
* TextViews. The first N views in this list are given the values of the first N columns
* in the from parameter.
*/
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
3.如果要显示的条目,不是但view,也不是双view,二是多view,那么就只能自定义适配器了,去继承BaseAdapter,在getView()中实现多view显示;
4.如果数据来自数据库,也就是以Cursor为参数,那么就需要使用上述以外的适配器了,SimpleCursorAdapter;
……………………………………………………………………………………………………………………………………………………………………
1.适配器有两个自接口:ListAdapter 、SpinnerAdapter
public interface ListAdapter extends Adapter;//
public interface SpinnerAdapter extends Adapter
2.适配器接口有一个基础实现抽象类,基础适配器:BaseAdapter
public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter
3.抽象类基础适配器,有两个子类和一个抽象子类:SimpleAdapter、ArrayAdapter<T>,CursorAdapter
public class SimpleAdapter extends BaseAdapter implements Filterable
public class ArrayAdapter<T> extends BaseAdapter implements Filterable
public abstract class CursorAdapter extends BaseAdapter implements Filterable,CursorFilter.CursorFilterClient
4.游标适配器抽象类,有个抽象子类:ResourceCursorAdapter,该抽象类有一个简单子类;
public abstract class ResourceCursorAdapter extends CursorAdapter
public class SimpleCursorAdapter extends ResourceCursorAdapter