【问题标题】:Displaying results of a SQL join in a ListActivity在 ListActivity 中显示 SQL 连接的结果
【发布时间】:2010-08-14 14:20:56
【问题描述】:

我需要在 ListView/ListActivity 中显示 SQL 连接的结果。

我已经创建了一个光标:

Cursor cursor = db.rawQuery(LIST_JOIN_SQL, null);

如果我遍历光标,结果正是我所期望的。但是,当我尝试使用 SimpleCursorAdapter 在 ListView 中显示这些结果时,我得到一个运行时异常,因为没有名为“id”的列。

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
R.layout.item, cursor, FROM, TO);

其中 FROM 和 TO 定义为:

private static String[] FROM = { "name", };
private static int[] TO = { R.id.name, };

现在我想我可能在这里使用了错误的方法,因为 SimpleCursorAdapters 似乎不适合显示连接结果。

您会在这里推荐什么方法?我只能使用 Android 1.6 API。

【问题讨论】:

    标签: android sqlite listview cursor listactivity


    【解决方案1】:

    尝试选择您的一列作为_ID 别名

    select col1 as _id from table
    

    【讨论】:

      【解决方案2】:

      要使用 CursorAdapter(或其任何子类),您需要有“_id”列; it's explicitly stated in the documentation。那是因为 CursorAdapter 大量使用了这个列。

      我推荐的是您认为(基于您的项目)更容易的两条路径:

      1. 在其中一个表中创建一个 _id 字段,以便在加入时得到一个 _id 字段。

      2. 实现您自己的 ListAdapter(从 BaseAdapter 开始),它本质上是一个 CursorAdapter,但不需要 _id 字段。

      我怀疑如果您控制数据库,#1 会更容易执行,但如果您无法更改数据库的架构,则必须执行#2。

      【讨论】:

        猜你喜欢
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-19
        相关资源
        最近更新 更多