Populate ExpandableListView with data from SQLite
Asked Answered
B

1

9

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
}
Belgravia answered 15/4, 2016 at 14:55 Comment(28)
dump movieCursor (DatabaseUtils#dumpCursor)Fenugreek
Prints everything just fine...Belgravia
override for testing bindGroupView / bindChildView and see if they are called, you can call DatabaseUtils#dumpCurrentRow inside themFenugreek
Results in 11 dumps being printed, all of them containing full and logic data... :/Belgravia
so if the views are bound, the only reason could be "white text on the white background" syndrome...Fenugreek
Yeah I thought of that too... Funny thing: I have a black background... Yet the text should be white... I'm gonna include the .xml files. Give me a sec...Belgravia
btw can you scroll it? 11 items should be enough to be taller than screen size...Fenugreek
btw #2: run adb shell dumpsys activity topFenugreek
It was made to fit the screen exactly when all groups were not expanded, but after the switch from "normal" objects to hold the data towards an SQLite DB I have never seen it since, and I have the feeling that 11 are more then I usually had...Belgravia
And what am I looking for when I ran the command?Belgravia
look for a view treeFenugreek
Ohhh I understand (Never used adb before)... There are no views in the ExpandableListViewBelgravia
wait, no views but bindGroupView was called 11 times? dump the view param in bindGroupViewFenugreek
Not sure what you mean by that... But view.toString() returns android.widget.LinearLayout{f18362f V.E...... ......ID 0,0-0,0} (Obviously with a different fancy number for each "dump")Belgravia
and dump of view.findViewById(R.id.spielplan_list_header_day) ?Fenugreek
and you are saying that android.widget.LinearLayout{f18362f is not in the output of dumpsys activity top?Fenugreek
System.out.println(view.findViewById(R.id.spielplan_list_header_day)); returns android.support.v7.widget.AppCompatTextView{d7de09c V.ED..... ......I. 0,0-0,0 #7f0e010f app:id/spielplan_list_header_day}. I updated the code to show where I'm getting this return fromBelgravia
Correct. As I understand it, the "highest" view is the ExpandableListView according to the adb shell dumpsys activity top command...Belgravia
find inside dumpsys output a android.widget.FrameLayout with id android:id/content and post only its child viewsFenugreek
Let us continue this discussion in chat.Belgravia
In one of your above comments you mention: "It was made to fit the screen exactly when all groups were not expanded", how did you do this? Also playing with the hierarchy viewer should give you a precise look on how that expandable list is present on the screen.Ticker
@Luksprog trial and error and some calculations... I simply adjusted the height so that the usual amount of elements would fit on the screen. Hierarchy viewer shows me that the ExpandableListView has no childs what so ever, and its parents are RelativeLayout -> Coordinator Layout -> Drawer Layout -> FitWindowsLinearLayout -> FrameLayout -> LinearLayout -> PhoneWindow$DecorViewBelgravia
Hard to tell what's happening from what you posted. I would recommend to get to a basic setup that manages to show your cursor in the expandable list: no height calculations, no colors, you could use some of the platform layouts(like android.R.layout.simple_list_item_1) instead of your current layouts for testing, no custom themes.Then work from there to your current code to see where it fails.Ticker
That's a good point, don't know why I didn't come up with that myself ^^. See the question code for the changes I've made. The result remains the same though. No childs for ExpandableListViewBelgravia
is your query "SELECT rowid _id,* FROM " + Statics.DBSHOWS + ";" correct? I think there is something wrong with your queryDairen
It should be... The cursor dump shows that the right data is returned..Belgravia
@Fenugreek is it maybe possible that it somehow doesn't get what data I'd like to use as a parent and thus has no parents (and thus has no childs)? I'm not sure that I specify it...Belgravia
@Barak since this is from your example, could you maybe have a look over this and tell me where I'm going wrong? I no 50 points is like nothing for you big guys, yet means a lot to me...Belgravia
B
-1

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.

Belgravia answered 27/4, 2016 at 12:43 Comment(1)
I like it about our job :DJadajadd

© 2022 - 2024 — McMap. All rights reserved.