【问题标题】:Using String as _id in CursorAdapter在 CursorAdapter 中使用字符串作为 _id
【发布时间】:2015-01-29 11:40:44
【问题描述】:

使用CursorAdapter 的要求是:

游标必须包含名为“_id”的列,否则此类将不起作用。

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 列?

【问题讨论】:

    标签: android sqlite android-cursoradapter


    【解决方案1】:

    如果我的数据库架构(在外部定义)不包含整数 _id 列,我有哪些选项可以强制非整数列或在查询输出中制造 _id 列?

    每个 sqlite 行都有一个唯一的ROWID,它是一个整数。您可以选择它作为 ID:

    String[] columns = new String[] { "rowid AS _id", ... };
    

    如果您的表恰好有一个INTEGER PRIMARY KEY 列,它将使用rowid 作为别名。

    参考:https://www.sqlite.org/lang_createtable.html#rowid

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 2021-03-18
      相关资源
      最近更新 更多