Populate ExpandableListView with data from SQLite

时间:2016-04-15 14:55:36

标签: android sqlite expandablelistview

The setup: I have a local SQLite database holding showtimes for movies. Some of the columns are "Date", "ID", "Time"...

The intention: I want to put all of these showtimes into a ExpandableListView while the dates are going to be the parent items.

The problem: Nothing is displayed. Nothing at all. And I struggle to understand why. Does anyone have an idea?

Some code: The most important stuff is up at the top, get's less and less important as you scroll down...

This is the code I'm trying to achieve this with which is called in the onCreate() of the Activity:

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

This is my 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;
    }

}

The two Override Methods bindChildView(...) and bindGroupView(...) were made for testing. As expected, the following output was printed:

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

1 个答案:

答案 0 :(得分:-1)

我与我们的爱好/工作有一种爱恨交织的关系:你有一些工作,在某些时候,某些东西似乎是完全独立的变化,然后你的东西不再起作用。这就是这里发生的事情,你无法猜到它:

随着我的设备更新到Marshmallow,它仍然可以读取我之前使用的ArrayList但无法读取数据库...为什么它仍然可以转储光标......?我不知道。但是一旦我发现你必须在旅途中请求权限,并实现了它,它就可以......或多或少。