有关 SimpleCursorTreeAdapter 和 getChildrenCursor() 的帮助

发布于 2024-10-20 13:36:03 字数 649 浏览 0 评论 0原文

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

我想使用 ExpandableListView 来显示这些数据。我需要在 SimpleCursorTreeAdapter 中实现 getChildrenCursor() 才能将其用于此目的,但我不确定如何执行此操作。我看过这个,我发现它使用 ManagedQuery,但我没有内容提供程序,因此无法使用它。从我的 明白,getChildrenCursor()的目的是获取一个只包含可以放入子项的数据的游标,但我看不出这个方法如何根据日期分隔条目,因为它只传递了一个 Cursor 作为参数。

I have an sqlite database with three columns: id, a date and a string. For a single date there can be multiple strings associated with it, so I have multiple rows with the same date just with different strings.

I want to use an ExpandableListView to show this data. I need to implement getChildrenCursor() in SimpleCursorTreeAdapter in order to use it for this purpose, but I am not sure how to do so. I have looked at this and I see that it uses managedQuery, but I do not have a content provider so I cannot use it. From what I understand, the purpose of getChildrenCursor() is to get a cursor with only the data that can be put in a child, but I can't see how this method can separate the entries according to their dates, since it's only passed a Cursor as a parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南城旧梦 2024-10-27 13:36:03
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);
    }
}
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);
    }
}
靑春怀旧 2024-10-27 13:36:03

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

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

I know it's 8 months too late, but still.

You can create cursor without a content provider. Open the SQLite database and do db.query(...) - this will create a cursor for you. It's the same way content providers create cursors.

鲜血染红嫁衣 2024-10-27 13:36:03

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

不应使用 ManagedQuery,因为它已在 API 11 中被弃用。

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

If you do not want to use a ContentProvider try running your query in an AsyncTask. Then use the changeCursor method inside onPostExecute to swap out the Cursor.

managedQuery should not be used as it has been depreciated in API 11.

The groupCursor object can be used to say get an "_id" for use in querying for its child data. like SELECT * FROM 'TABLE' WHERE ID = ?. the "?" being an ID column from the group cursor, which is most likely going to be used as a foreign key on another table. If you're still confused try searching for "Database Normalization" on google.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文