Home » Android » android – Populate ExpandableListView with data from SQLite

android – Populate ExpandableListView with data from SQLite

Posted by: admin June 15, 2020 Leave a comment

Questions:

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
}
How to&Answers:

I have a love-hate relationship with our hobby/job: You have something working, at some point, something that seems totally independent changes, and then later your something is not working again. This is what happened here, and there was no way you could have guessed it:

With my Device updating to Marshmallow it could still read the ArrayList I used before but couldn’t read the database anymore… Why it could still dump the cursor…? I don’t know. But as soon as I figured out that you must request permissions on the go, and implemented that, it works… More or less anyway.