zoukankan      html  css  js  c++  java
  • android 适配器Adpter的使用总结 之 CursorAdpter

    转载请标明出处: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的使用总结>中的三步来做,适配器的模块:

    publicclass 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所指向的数据。
    publicvoid 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;
    }

    publicvoid 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));
    }
    }
    }

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

    publicclass ListCursor extends Activity{
    private ListView listView;
    private Cursor cursor;
    @Override
    protectedvoid 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;
    }
    }
    有什么不对的地方还望大牛多多指教!
    相关代码发布在QQ群:Android中高级技术免费培训QQ群(118949422)
    很晚了,睡觉去了,明天有机会再写。
  • 相关阅读:
    第12章 项目采购管理
    C# 利用xml动态生成带图标菜单
    C#正则表达式整理备忘
    IE8"开发人员工具"使用详解下
    拖盘控件notifyIcon演示例程
    多列选择框控件checkedListBox演示程序
    树形框treeView演示程序
    错误提示控件errorProvider演示例程
    IE8“开发人员工具”使用详解上
    c#中分割字符串的几种方法
  • 原文地址:https://www.cnblogs.com/tanlon/p/2085562.html
Copyright © 2011-2022 走看看