转载请标明出处:http://www.cnblogs.com/tanlon/archive/2011/06/20/2085562.html

通过前面两篇文章:

Android 适配器Adapter的学习:http://www.cnblogs.com/tanlon/archive/2011/05/21/2053009.html

Android 适配器Adpter的使用总结:http://www.cnblogs.com/tanlon/archive/2011/06/20/2084901.html

我们初步了解了适配器的继承以及关系,在这里我们继续了解关于和数据库相关的适配器CursorAdpter。

我们知道BaseAdpter实现SpinnerAdapter和ListAdapter接口,而CursorAdpter则是继承了 BaseAdpter,并且衍生了ResourceCursorAdapter,又由ResourceCursorAdapter衍生了 SimpleCursorAdapter。

另外还有BaseExpandableListAdapter则是实现了ExpandableListAdpter、HeterogeneousExpandableList接口,同理CursorTreeAdapter继承了BaseExpandableListAdapter,并且衍生出了ResourceCusorTreeAdapter,由ResourceCusorTreeAdapter又衍生出了SimpleCursorTreeAdapter。

关于BaseExpandableListAdapter这一节我留着下一篇来讲。

在这里我拿CursorAdapter来做demo:

还是按照<Android 适配器Adpter的使用总结>中的三步来做,适配器的模块:

public class ListCursorAdpter extends CursorAdapter{

private LayoutInflater layoutInflater;
// 构造函数。
public ListCursorAdpter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
// 构造函数。 每当数据库的数据发生改变时,适配器将调用requery()重新查询以显示最新的数据。
public ListCursorAdpter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
// TODO Auto-generated constructor stub
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// 重用一个已有的view,使其显示当前cursor所指向的数据。
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
setChildView(view, cursor);
}
// 新建一个视图来保存cursor指向的数据
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
View view = layoutInflater.inflate(R.layout.listsimple_item, null );
setChildView(view, cursor);
return view;
}

public void setChildView(View view, Cursor cursor){
TextView title
= (TextView) view.findViewById(R.id.listitem_title);
TextView content
= (TextView) view.findViewById(R.id.listitem_content);
if (cursor.moveToNext() && cursor.getCount() > 0 ) {
if (cursor.getString( 7 ) != null )
title.setText(cursor.getString(
7 ));
if (cursor.getString( 1 ) != null )
content.setText(cursor.getString(
1 ));
}
}
}

接着就是实例化数据填充对象(这里以媒体数据库里的数据为例):

public class ListCursor extends Activity{
private ListView listView;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
// 查询所有的媒体文件
cursor = allSongs();
if (cursor != null )
{
startManagingCursor(cursor);
// 我们将获得的Cursor对象交与Activity 来管理,这样Cursor对象的生命周期便能与当前的Activity自动同步,省去了自己管理Cursor
}
listView
= (ListView) findViewById(R.id.listview_simple);
ListCursorAdpter listCursorAdpter
= new ListCursorAdpter( this , cursor, true );
listView.setAdapter(listCursorAdpter);
}

private Cursor allSongs() {
// TODO Auto-generated method stub
if (cursor != null )
{
return cursor;
}
ContentResolver resolver
= getContentResolver(); // 获取ContentResolver对象
cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null , null , null , MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
return cursor;
}
}

有什么不对的地方还望大牛多多指教!

更多相关文章

  1. Android数据的四种存储方式
  2. Android旋转屏幕不销毁数据的方法
  3. Android使用SQLiteDatabase直接存取数据与图像
  4. android 数据库操作 GreenDAO 第三方开源项目
  5. android 使用Okhttp封装上传JSON格式数据的工具类
  6. Android 数据库 短信 监听
  7. android中常见的二种数据解析方法----XML和Json
  8. Android 复习笔记之图解ContentProvider实现数据共享
  9. Android 继承SQLiteOpenHelper自定义DBHelper存取数据与图像

随机推荐

  1. Android(安卓)AutoCompleteTextView的使
  2. Android(安卓)studio 2.0集成NDK
  3. android 自定义照相机Camera黑屏
  4. 使用 Android(安卓)Studio 搭建安卓开发
  5. Android(安卓)studio新建项目之后由于gra
  6. osg for android 学习之纹理丢失解决
  7. Android写入内部存储和sd卡
  8. Android(安卓)常见分辨率(mdpi、hdpi 、xh
  9. android5.1-在系统设置里添加设置选项 以
  10. Android应用程序访问远程数据库(mysql) i