【问题标题】:Help with SimpleCursorTreeAdapter and getChildrenCursor()帮助 SimpleCursorTreeAdapter 和 getChildrenCursor()
【发布时间】:2011-03-05 06:33:49
【问题描述】:

我有一个包含三列的 sqlite 数据库:id、日期和字符串。对于单个日期,可以有多个与之关联的字符串,因此我有多个具有相同日期的行,只是具有不同的字符串。

我想使用 ExpandableListView 来显示这些数据。我需要在 SimpleCursorTreeAdapter 中实现 getChildrenCursor() 才能将其用于此目的,但我不知道该怎么做。我查看了this,发现它使用 managedQuery,但我没有内容提供程序,所以我无法使用它。从我understand 来看,getChildrenCursor() 的目的是获取一个仅包含可以放入子项的数据的游标,但我看不出这种方法如何根据日期分隔条目,因为它是只传递了一个 Cursor 作为参数。

【问题讨论】:

  • 更新:在对代码进行了多次争论之后,我没有去任何地方,我放弃了,只使用了一个 ListView。
  • 我也遇到了与您在此处提到的完全相同的问题。你能解决同样的问题吗?
  • 我在这里做类似的事情stackoverflow.com/questions/10611927/…

标签: android sqlite


【解决方案1】:
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

    public MyExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
            int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
            int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,
                childrenTo);
    }

    @Override
    @SuppressWarnings("deprecation")
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        // Given the group, we return a cursor for all the children within that group 
        // Return a cursor that points to this contact's phone numbers
        Uri.Builder builder = People.CONTENT_URI.buildUpon();
        ContentUris.appendId(builder, groupCursor.getLong(mGroupIdColumnIndex));
        builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
        Uri phoneNumbersUri = builder.build();
        // The returned Cursor MUST be managed by us, so we use Activity's helper
        // functionality to manage it for us.
        return managedQuery(phoneNumbersUri, mPhoneNumberProjection, null, null, null);
    }
}

【讨论】:

  • 请仔细阅读我的问题。我不想使用内容提供者。
【解决方案2】:

我知道已经晚了 8 个月,但仍然如此。

您可以在没有内容提供者的情况下创建光标。打开 SQLite 数据库并执行 db.query(...) - 这将为您创建一个游标。这与内容提供者创建游标的方式相同。

【讨论】:

    【解决方案3】:

    如果您不想使用 ContentProvider,请尝试在 AsyncTask 中运行您的查询。然后使用 onPostExecute 中的 changeCursor 方法来换出 Cursor。

    managedQuery 不应使用,因为它在 API 11 中已被贬低。

    groupCursor 对象可用于表示获取“_id”以用于查询其子数据。比如 SELECT * FROM 'TABLE' WHERE ID = ?。这 ”?”是来自组游标的 ID 列,很可能将用作另一个表的外键。如果您仍然感到困惑,请尝试在 google 上搜索“Database Normalization”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 2011-06-03
      • 2011-11-02
      • 2011-06-02
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多