Search code examples
androidsqliteandroid-cursoradapter

How to retrieve data from child table using CursorLoader which loads data from main table?


My app is using CursorLoader to load data from main table. The data will be displayed in ListView with the help of CursorAdapter. The view displayed in ListView also requires data from child table. The relationship of the tables is one-to-many.

  • If I join both tables during the query, the combined records will be as many as child records, thus in ListView, it will display multiple item for one single record in the main table.

  • If I don't join the table, I am not sure what the best way to retrieve child records after the CursorLoader has delivered the data via the cursor. Anyone able to help me out?


Solution

  • You should try the join option and set your sort order for your query to make sure that the results are sorted by the results in the main table.

    Afterwards, you can use an ExpandableListView to show the main item and all the child items underneath to indicate the "one-to-many" relationship in a good way. Of course you need to write your adapter appropriately to handle this case.