这是使用 CursorAdapter 的要求那:
The Cursor must include a column named "_id" or this class will not work.
从getItemId()
可以清楚签名和 CursorAdapter 源,该列应为整数类型:
/**
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
if (mDataValid && mCursor != null) {
if (mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIDColumn);
} else {
return 0;
}
} else {
return 0;
}
}
我真的很想能够使用另一个包含文本(UUID 字符串)的列作为我的 _id
,但看起来这不可能直接实现。
如果我的数据库模式(外部定义)不包含整数 _id
列,我有哪些选项可以强制非整数列,或制造 _id
查询输出中的列?
最佳答案
If my database schema (which is defined externally) does not include an integer _id column, what options do I have for coercing a non-integer column, or manufacturing an _id column in the query output?
每个 sqlite 行都有一个唯一的 ROWID
,它是一个整数。您可以选择它作为 ID:
String[] columns = new String[] { "rowid AS _id", ... };
如果您的表恰好有一个 INTEGER PRIMARY KEY
列,它将使用 rowid 作为别名。
关于android - 在 CursorAdapter 中使用 String 作为 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28213851/