Android中常用的适配器
1.常用的适配器有ArrayAdapter,SimpleAdapter,SimpleCursorAdapter 这三个,他们都是继承于BaseAdapter 。
ArrayAdapter
1.final String[] data = { "重打最后一笔", "重打任意一笔", "重打交易明细 ", };
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter adapter = new ArrayAdapter
(this,android.R.layout.simple_list_item_1, data);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
// 用户点击了第一行
if (arg2 == 0)
{
}
// 用户点击了第二行
else if (arg2 == 1)
{
}
// 用户点击了第三行
else if (arg2 == 2) {
} else {// 用户点击了第四行
}
}
});
SimpleAdapter
2. final List
ListView lv = (ListView) findViewById(R.id.list);
Map map = new HashMap();
/*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);
}*/
map.put("TITLE", "账户管理");
map.put("PIC", R.drawable.jian);
values.add(map);
map = new HashMap();
map.put("TITLE", "IC卡设置");
map.put("PIC", R.drawable.jian);
values.add(map);
map = new HashMap();
map.put("TITLE", "时间设置");
map.put("PIC", R.drawable.jian);
values.add(map);
SimpleAdapter adapter = new SimpleAdapter(AdminActivity.this,
values, R.layout.itemno,
new String[] {"TITLE","PIC"}, new int[] {R.id.title, R.id.pic});
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView> arg0, View arg1, int
arg2,long arg3)
{
if(arg2 == 0)
{
}
else if(arg3 == 1)
{
}
else if(arg3 == 2)
{
}
else
{
}
}
});
3.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。
4.自定义适配器
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;
}
}