Search code examples
javaandroiddatabasesqliteexpandablelistview

How do I connect an existing SQLite database to an ExpandableListView?


I've been trying to prepare custom Views to display SQLite database entries during a whole week without success.

I already tried the default list and Room Persistence Library approaches and even considered a JSON/GSON alternative, but to populate lists within lists with basic text strings seemed to be the easiest attempt meanwhile.

Basically, now I'm trying to mix two samples at once:

I've been able to add data from my database and to mix them up to some extent, but I ain't getting how to connect the database fields to the custom Views on both levels of the list.

After I reached the ExpandableListView on the MainActivity.java, I got lost and I think the code may have some duplicated methods, maybe a confusion between dataAdapter and the use of listAdapter and the HashMap<String, List<String>>() is probably wrong, but I don't have a clue about how to solve issues like these as of now, so, I ask for some help please to handle this situation.


Solution

  • For those who are trying to fetch data from a prepopulated SQLite database into an ExpandableListView, you can simply use this sample code I created after asking this question here and there.

    You'd just need to replace the database in this project by yours and refactor the respective fields to match your database name, table and items so as it fits your needs.

    Basically, with a SimpleCursorTreeAdapter, you'll be able to select two levels of data (group and child) from your database and SQLiteAssetHelper will let you to load it from a preexisting file put inside the assets folder (/app/src/main/assets/databases/).