android - 在 CursorAdapter 中使用 String 作为 _id

标签 android sqlite android-cursoradapter

这是使用 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 作为别名。

引用:https://www.sqlite.org/lang_createtable.html#rowid

关于android - 在 CursorAdapter 中使用 String 作为 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28213851/

相关文章:

android - Android 正在读取 SQLite 中的 iOS 日期(NSDate)

android - 带有 CursorAdapter 的 ListView

android - 从 strings.xml 引用 gradle.properties 中的日文/中文字符串会导致未知行为

sqlite - SQLite 有游标吗?

c - 如何将用户输入作为sql语句的参数

android - ListView 的这个自定义 CursorAdapter 是否为 Android 正确编码?

Android - 自定义 CursorAdapter 在 ViewFlipper 翻转时不断调用 BindView

android - 无法在Flutter中设置SetState()

android - 如何更改Android中状态栏的颜色?

android - 在 Android 中使用 OnClickItemListener 时 Android 中的 OutOfMemoryError