android - 帮助 SimpleCursorTreeAdapter 和 getChildrenCursor()

标签 android sqlite

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

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

最佳答案

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);
    }
}

关于android - 帮助 SimpleCursorTreeAdapter 和 getChildrenCursor(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5202161/

相关文章:

sql - 试图为JOIN查询引入一个简单的新条件

ruby-on-rails - 如何在设置 Ruby on Rails 时安装 SQLite3?

c# - 如何在 xamarin iOS 中更新 sqlite 数据库表

android - 设备屏幕锁定时计时器在服务中变慢

android - 如何将@2x @3x 图像集成到 React Native 项目(Android 和 IOS)?

java - 如何在服务中使用 ShowcaseView 库或类似的东西?

android - 子模块中的 Gradle 包装器版本与项目根目录中的不同

sqlite - 如何将行号保存到 SQLite 中的表中?

android - 捕获图像后尝试设置图像时获取 "Caused by: java.lang.NullPointerException: uri"

android - LocalStorage 与 Sqlite 数据库