使用来自 SQLite 的数据填充 ExpandableListView

Populate ExpandableListView with data from SQLite

设置: 我有一个保存电影放映时间的本地 SQLite 数据库。其中一些列是 "Date"、"ID"、"Time"...

意向: 我想将所有这些放映时间放入 ExpandableListView,而日期将成为父项。

问题:什么都不显示。什么都没有。我很难理解为什么。有人有想法吗?

一些代码:最重要的内容在顶部,向下滚动时越来越不重要...

这是我试图实现此目的的代码,它在 Activity 的 onCreate() 中调用:

String sql = "SELECT rowid _id,* FROM " + Statics.DBSHOWS + ";";
Cursor movieCursor = database.rawQuery(sql,null);
movieCursor.moveToFirst();
adapter = new ExpandableListAdapter(movieCursor, this,
            android.R.layout.simple_list_item_1,
            android.R.layout.simple_list_item_2,
            new String[]{Statics.DBSHOWS_DATE},
            new int[]{android.R.id.text1},
            new String[]{Statics.DBSHOWS_TIME, Statics.DBSHOWS_ID},
            new int[]{android.R.id.text1, android.R.id.text2});

movieListView.setAdapter(adapter);

这是我的 ExpandableListAdapter:

public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
    Context context;

    public ExpandableListAdapter(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);
        this.context = context;
    }



    @Override
    protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
        super.bindChildView(view, context, cursor, isLastChild);
        System.out.println("Dumping form Childview");
        DatabaseUtils.dumpCurrentRow(cursor);
    }

    @Override
    protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
        super.bindGroupView(view, context, cursor, isExpanded);
        if(view==null){
            System.out.println("View is null!!");
        }
        System.out.println("Dumping form Groupview");
        DatabaseUtils.dumpCurrentRow(cursor);
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        int dateInMillis = groupCursor.getInt(groupCursor.getColumnIndex(Statics.DBSHOWS_DATE));
        Cursor childCursor = DataHandler.getShowsForDate(dateInMillis);
        childCursor.moveToFirst();
        return childCursor;
    }

}

两个Override方法bindChildView(...)bindGroupView(...)是为了测试而制作的。正如预期的那样,打印了以下输出:

Dumping form Groupview
0 {
    _id=1
    Id=117451
    Date=15.04.2016
    Time=20:15
    Movie_Id=2181
    Hall=0
    Cards_Sold=0
}

我和我们的 hobby/job 又爱又恨:你有一些东西在工作,在某些时候,一些看起来完全独立的东西发生了变化,然后你的东西又不工作了。这就是这里发生的事情,你不可能猜到:

随着我的设备更新到 Marshmallow,它仍然可以读取我以前使用的 ArrayList 但不能再读取数据库...为什么它仍然可以转储光标...?我不知道。但是一旦我发现您 必须 随时随地请求权限并实施该权限,它就可以正常工作...无论如何。