Android 常用适配器总结
时间:2022-12-02 06:30:00
一,适配器.顾名思义,有些数据是合适的,适合View上显示。可视为是
界面数据绑定的理解。它操作的数据通常是一些更复杂的数据,如数组、链表、数据库、集合等。适配器就像一个显示器,以可接受的方式显示复杂的东西。那么,适配器是如何处理数据并显示它的呢。事实上,这很简单,直率地说,适配器也是一个类别,在类别中,它实现了父亲的这些方法:
publicint getCount() ///获取数据的行数
public Object getItem(int position)//根据position记录某一行
public long getItemId(int position)////获得某个记录ID
///下面的方法是最重要的。与其他方法相比,它的显式定义。适配器将以什么方式显示我们填写的数据?在自定义的适配器中,我们通常会写一份布局文件publicView getView(int position, View convertView, ViewGroup parent)
有三种常用的适配器,当然不包括自定义的适配器,哪三种是ArrayAdapter,SimpleAdapter,SimpleCursorAdapter 他们都继承了这三个。BaseAdapter
其中以ArrayAdapter最简单的,只能显示一行字。SimpleAdapter具有最佳扩展性,可自定义各种效果。SimpleCursorAdapter可以认为是SimpleAdapter数据库的简单组合可以以列表的形式显示数据库的内容。第二,对于前两个适配器,他们的数据来源无非是String[]或者List 。下面我们列举两个例子:例一,数组作为数据源填充ArrayAdapterpublic class Example extends ListActivity{
String[] sex = new String(){"男",女}//数据源
ArrayAdapteradapter;///数组适配器使用泛型
public voidonCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceStat);
//在对适配器进行初始化时,顺便将数据源装载到适配器中, //this.android.R.Layout.Simple_List_Item_1这句话
//意思是将数据源以系统定义的风格放入适配器.
adapter=newArrayAdapter
this.setAdapter(adapter);//这是一个控件类,可以直接将适配器绑定到自己的对象中。
}
}
例二:List填充作为数据源SimpleAdapter
ListView list = (ListView)findViewById(R.id.MyListView);
///生成动态数组并转载数据
ArrayList> mylist = newArrayList>();
for(int i=0;i<30;i )
{
HashMapmap = new HashMap();
map.put("ItemTitle","This is Title...");
map.put("ItemText","This is text...");
mylist.add(map);}
//生成适配器,数组===》ListItem
SimpleAdapter mSchedule = new SimpleAdapter(this, ///没有解释 mylist,///数据源 R.layout.my_listitem,//ListItem的XML实现 ///动态数组和ListItem对应的子项
new String[]{"ItemTitle", "ItemText"}, //ListItem的XML文件中的两个TextView IDnew int[] {R.id.ItemTitle,R.id.ItemText});
//添加并显示
list.setAdapter(mSchedule);
}
三、说两个例子都不难,都是我们经常看到的一些用法,所以对于SimpleCursorAdapter怎么用?SimpleCursorAdapter一般主要用于数据库,其数据源一般是通过数据库查询获得的Cursor 让我们来看看下面的例子:
String uriString = “content://contacts/people/”;
Cursor myCursor =managedQuery(Uri.parse(uriString), null, null, null, null);
String[] fromColumns = new String[]{People.NUMBER, People.NAME};int[] toLayoutIDs = new int[] {R.id.nameTextView, R.id.numberTextView};
SimpleCursorAdapter myAdapter;
myAdapter=newSimpleCursorAdapter(this,R.layout.simplecursorlayout,myCursor,fromColumns,
toLayoutIDs);///传入当前上下文,一个layout资源,一个游标和两个数组:一个包含使用列
//名称,另一个(相同大小)数组包含View中的资源ID,显示相应列的数量
据值。
myListView.setAdapter(myAdapter);
我们绑定了一个游标ListView使用自定义layout显示每一个Item。
我们来定义一下自己的适配器。
为什么要定义自己的适配器?原因是,当我们想使用其他一些显示方法,或者我们需要的,这是必要的DIY了。
首先,我们定义一个类让它继承BaseAdapter,让它实现里面提到的方法。那么这个自定义适配器就好了。上面介绍了一些方法,这里就不赘述了。
public class ImageAdapter extendsBaseAdapter {
private Context mcontext;};
//构造函数中有两个参数,一个是数据源,另一个是上下文。
public ImageAdapter(Integer[] imgIds,Context c){
mcontext=c;
imageIds=imgIds;
}
publicint getCount() {
// TODO Auto-generated method stub
return imageIds.length;
}
publicObject getItem(int position) {
// TODO Auto-generated method stub
return null;
}
publiclong getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
///主要工作在这里,可以定制布局,这里就不多说了。
publicView getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageview = newImageView(mcontext);
imageview.setImageResource(imageIds[position]);
imageview.setLayoutParams(newGallery.LayoutParams(120,120));
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageview;
}
}