CursorAdapter

CursorAdapter继承于BaseAdapter,它是个虚类,它为cursor和ListView提供了连接的桥梁。

public abstract class CursorAdapter extends BaseAdapter

注意cursor的必须要有个命名为"_id"的列。比如Contacts._ID就为"_id"

必须实现以下函数:

abstract View newView(Context  context, Cursor  cursor, ViewGroup  parent)    abstract void  bindView(View  view, Context  context, Cursor  cursor)

newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的
数据增加后,回调用该函数来生成与新增数据相对应的view。
bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的

总的来说应该是在调用bindView如果发现view为空会先调用newView来生成view。

Code

public class MySqliteOpenhelper extends SQLiteOpenHelper {                                                                        public MySqliteOpenhelper(Context context,  int version)     {         super(context, "dianhuaben.db", null, version);     }     @Override    public void onCreate(SQLiteDatabase db)     {//注意:使用CursorAdapter时,创建表必须有以_id为列名的列         String sql = "CREATE TABLE dhb (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))";         db.execSQL(sql);     }     @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)     {     } }
public void createCursorAdapter(Cursor cursor)     { //游标适配器,构造方法,传入cursor         mAdapter = new CursorAdapter(this, cursor)         {//重写两个方法             @Override            public View newView(Context context, Cursor cursor, ViewGroup parent)             {//找到布局和控件                 ViewHolder holder = new ViewHolder();                 LayoutInflater inflater = getLayoutInflater();                 View inflate = inflater.inflate(R.layout.listview_item, null);                 holder.item_tv_name = (TextView) inflate.findViewById(R.id.item_tv_name);                 holder.item_tv_phone = (TextView) inflate.findViewById(R.id.item_tv_phone);                 inflate.setTag(holder);                 return inflate;//返回的view传给bindView。             }                                                                           @Override            public void bindView(View view, Context context, Cursor cursor)             {//                把数据设置到界面上                 ViewHolder holder = (ViewHolder) view.getTag();                 String name = cursor.getString(cursor.getColumnIndex("name"));                 String phone = cursor.getString(cursor.getColumnIndex("phone"));                 holder.item_tv_name.setText(name);                 holder.item_tv_phone.setText(phone);             }                                                                   };                                                         };

SimpleCursorAdapter

简单提及一下

public void createSimpleCursorAdapter(Cursor cursor)     {// SimpleCursorAdapter继承了CursorAdapter继承了BaseAdapter         String[] from = {TABLE_NAME_NAME,TABLE_NAME_PHONE};//列名与控件id一一对应         int[] to = {R.id.item_tv_name,R.id.item_tv_phone};         //用的是SimpleCursorAdapter,用法和simpleAdapter相似         mAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.listview_item, cursor, from, to);     }

使用SimpleCursorAdapter,时,创建表必须有以_id为列名的列

我是天王盖地虎的分割线

更多相关文章

  1. Android(安卓)TTS学习――TTS初体验(中)
  2. Android(安卓)水波纹扩散效果
  3. Android(安卓)输入系统解析 (2)
  4. Android的Layout --- 布局
  5. Android(安卓)动画 - ScaleAnimation 缩放动画使用(附图)
  6. 开发人员注意——Android(安卓)5.0 API有哪些变化
  7. Android开发常见错误及技巧
  8. android中使用百度定位sdk实时的计算移动距离
  9. ARM cortex A8/9 - Android(安卓)NDK - NEON介绍以及优化

随机推荐

  1. android switch模块
  2. Android(安卓)获取远程图片与本地图片缓
  3. Android(安卓)中LayoutInflater(布局加载
  4. Android之3D物理引擎
  5. android 常用控件的使用 TextView EditTe
  6. UE4 安卓打包纹理格式选择
  7. Android(安卓)技术专题系列之三 -- 编译(bui
  8. Android中Notification的framework层讲解
  9. [译] 在 Android(安卓)使用协程(part III
  10. android之layout_weight体验(实现按比例