Android对数据库表的一个约定:每张表都应该至少有_id这列
来源:互联网 发布:淘宝商品老是被删除 编辑:程序博客网 时间:2024/05/16 09:28
Android对数据库表有一个约定。就是每张表都应该至少有_id这列。ListView在使用CursorAdapter及其子类适配 cursor的时候,会默认的获取 _id 这列的值,如果你建的表没有 _id这列或者你的cursor中没有_id这列(查询时的projection中没有_id)就报错了。所以使用CursorAdapter及其子类的时候一定要使查询时的projection包含_id。CursorAdapter中相关代码如下:
1.注释/** * Adapter that exposes data from a {@link android.database.Cursor Cursor} to a * {@link android.widget.ListView ListView} widget. The Cursor must include * a column named "_id" or this class will not work. */2.构造方法 public CursorAdapter(Context context, Cursor c) { init(context, c, true); }public CursorAdapter(Context context, Cursor c, boolean autoRequery) { init(context, c, autoRequery); } protected void init(Context context, Cursor c, boolean autoRequery) { boolean cursorPresent = c != null; mAutoRequery = autoRequery; mCursor = c; mDataValid = cursorPresent; mContext = context; mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1; mChangeObserver = new ChangeObserver(); if (cursorPresent) { c.registerContentObserver(mChangeObserver); c.registerDataSetObserver(mDataSetObserver); } }3.public void changeCursor(Cursor cursor) { if (cursor == mCursor) { return; } if (mCursor != null) { mCursor.unregisterContentObserver(mChangeObserver); mCursor.unregisterDataSetObserver(mDataSetObserver); mCursor.close(); } mCursor = cursor; if (cursor != null) { cursor.registerContentObserver(mChangeObserver); cursor.registerDataSetObserver(mDataSetObserver); mRowIDColumn = cursor.getColumnIndexOrThrow("_id"); mDataValid = true; // notify the observers about the new cursor notifyDataSetChanged(); } else { mRowIDColumn = -1; mDataValid = false; // notify the observers about the lack of a data set notifyDataSetInvalidated(); } }
其中cursor.getColumnIndexOrThrow(String columnName)如下:
/** * Returns the zero-based index for the given column name, or throws * {@link IllegalArgumentException} if the column doesn't exist. If you're not sure if * a column will exist or not use {@link #getColumnIndex(String)} and check for -1, which * is more efficient than catching the exceptions. * * @param columnName the name of the target column. * @return the zero-based column index for the given column name * @see #getColumnIndex(String) * @throws IllegalArgumentException if the column does not exist */ int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;
所以只要Cursor不为空,Cursor中必须存在_id列,CursorAdapter及其子类才能正常工作。
- Android对数据库表的一个约定:每张表都应该至少有_id这列
- 关于每一个数据库表都应该有一个单一的字段作为主键的讨论
- 利用 JSP 中的反射机制封装一个 Servlet ,就不用每张表的每个方法都写一个 Servlet
- SQL查询数据库中每张表的记录数!
- SQL查询数据库中每张表的记录数
- 统计数据库中每张表的记录数统计
- 快速查看数据库每张表的大小(存储过程)
- 创建一个目录列表进行每张sheet表的链接
- 3.6.2 每张表一个类层次
- 3.6.3 每张表一个子类
- 表结构设计-每张表都需要有外键和type
- 抽奖,a概率0.2,b概率0.3,a,b都至少有一个的期望
- 每个团队都应该有一个Appfuse式的项目
- 每个团队都应该有一个Appfuse式的项目
- 每个人,都应该有一个自豪的作品代表自己
- 所有程序员都应该至少读上两遍的十篇论文
- 所有程序员都应该至少读上两遍的十篇论文
- 所有程序员都应该至少读上两遍的十篇论文
- MSDE 安装参数说明
- c/c++程序的内存分配
- 转载:Sensor传感器源码的阅读与应用开发简单实例
- USACO section 1.2 Name That Number
- pb的多态思想
- Android对数据库表的一个约定:每张表都应该至少有_id这列
- sprintf详解
- 以程序的方式操纵NTFS的文件权限
- .NET打包安装MSDE以及安装和卸载
- Poj2266/Poj2270(四元树) .
- 斜堆(Skew Heap)
- tmux 多窗口管理器
- 算法(数据结构)
- 最牛的程序员的自述