之前的博客中,我简要的说了一下ArrayAdapter以及SimpleAdapter的使用,也提到了SimpleCursorAdapter,一般是用于将数据库中的数据加载到ListView中,而BaseAdapter是抽象类,需要我们去自定义一个Adapter类,来直接继承BaseAdapter类,或者
继承ArrayAdapter或SimpleAdapter,下边我简要的说一下SimpleCursorAdapter和BaseAdatper的使用。
SimpleCursorAdapter的使用,就是把将Cursor对象中的数据构建成Item,需要注意的是数据库表中的主键名称 必须为_id,为了方便取得id的主键值。下边是一段SimpleCursorAdapter的示例代码:
上面的程序就是将数据库中contact表中的数据查询出来,绑定到ListView进行显示,现在简单说一下SimpleCursorAdapter的构造方法的参数含义:
SimpleCursorAdapter (Context context, int layout, Cursor cursor, String[] from, int[] to,int flags)
context:应用程序上下文,具体来说就是ListView所在的上下文当中
layout:布局文件的资源定位标识符,也就是说标识了ListView中的item。那么这个布局文件至少包含了参数“to”中的传进来值。
cursor:数据库游标,如果游标不可用则为null
from:一个字符串数组,里边是列名字列表,表示着你要绑定到UI上的列。如果游标不可用则为null。
to:展示参数“from”中的列,也就是说ListView中的视图显示的是参数“from”的列值,这些视图应该都是TextView。如果游标不可用则为null。
flags:这个标志用来决定该适配器的行为。
下面讲一下BaseAdapter的使用:
BaseAdapter用于ListView(实现指定的ListAdapter接口)和Spinner(实现指定的SpinnerAdapter接口)的共同实现一个公共基类适配器。
直接子类: ArrayAdapter<T>, CursorAdapter, SimpleAdapter 间接子类 : ResourceCursorAdapter, SimpleCursorAdapter
需要重写getView方法
示例代码:
继承ArrayAdapter或SimpleAdapter,下边我简要的说一下SimpleCursorAdapter和BaseAdatper的使用。
SimpleCursorAdapter的使用,就是把将Cursor对象中的数据构建成Item,需要注意的是数据库表中的主键名称 必须为_id,为了方便取得id的主键值。下边是一段SimpleCursorAdapter的示例代码:
//1.获得ListView对象
ListView lv=(ListView) this.findViewById(R.id.lv1);
//2.构建Simplecursoradapter适配器
//2.1 item data(通过SQLiteDatabase对象打开或创建一个ContactsDB.db的数据库,访问模式为私有模式,不与其他应用进行共享)
SQLiteDatabase sqlb=openOrCreateDatabase("ContactsDB.db", Context.MODE_PRIVATE, null);
//2.2 执行数据库查询功能,查询contact表中的数据,并获得查询出来的记录的Cursor对象。
Cursor cor=sqlb.rawQuery("select * from contact", null);
//2.3 item view
scur=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cor,
new String[]{"name","phone"},
new int []{android.R.id.text1,android.R.id.text2},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);//决定了Adapter的行为,数据更新后,是否更新ListView
//3.关联adapter
lv.setAdapter(scur);
上面的程序就是将数据库中contact表中的数据查询出来,绑定到ListView进行显示,现在简单说一下SimpleCursorAdapter的构造方法的参数含义:
SimpleCursorAdapter (Context context, int layout, Cursor cursor, String[] from, int[] to,int flags)
context:应用程序上下文,具体来说就是ListView所在的上下文当中
layout:布局文件的资源定位标识符,也就是说标识了ListView中的item。那么这个布局文件至少包含了参数“to”中的传进来值。
cursor:数据库游标,如果游标不可用则为null
from:一个字符串数组,里边是列名字列表,表示着你要绑定到UI上的列。如果游标不可用则为null。
to:展示参数“from”中的列,也就是说ListView中的视图显示的是参数“from”的列值,这些视图应该都是TextView。如果游标不可用则为null。
flags:这个标志用来决定该适配器的行为。
下面讲一下BaseAdapter的使用:
BaseAdapter用于ListView(实现指定的ListAdapter接口)和Spinner(实现指定的SpinnerAdapter接口)的共同实现一个公共基类适配器。
直接子类: ArrayAdapter<T>, CursorAdapter, SimpleAdapter 间接子类 : ResourceCursorAdapter, SimpleCursorAdapter
需要重写getView方法
示例代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position,convertView,parent);
}
private View createViewFromResource(int position, View convertView, ViewGroup parent) {
Log.i("info", "convertView:"+convertView);
ViewHolder vh=null;
//生成的View参考resource模板构建的view对象返回值为resource模板的根元素
if(convertView==null){
vh=new ViewHolder();
convertView=View.inflate(getContext(),R.layout.simpleitem,null);
vh.imageview=(ImageView) convertView.findViewById(R.id.img);
vh.textview=(TextView) convertView.findViewById(R.id.txtname);
convertView.setTag(vh);
}else{
vh=(ViewHolder)convertView.getTag();
}
City c=getItem(position);
vh.imageview.setImageURI(Uri.fromFile(new File(c.getLogo())));
vh.textview.setText(c.getName());
return convertView;
}