lundi 29 décembre 2014

Android ListView with Many-To-Many relationship

first question to SO, so please let me know if I'm stepping on any toes :)


This is a simplified version of my database:



CREATE TABLE products (_id INTEGER PRIMARY KEY, name STRING);

CREATE TABLE product_tag (product_id INTEGER, tag STRING, PRIMARY KEY(product_id, tag));


In one of my activities, I would like to list all products (one row per product) with all tags contained in the row, e.g.



bananas [yellow] [fruit]
ketchup [condiment] [red] [tomato]
mustard [yellow] [condiment]


The options I have considered are:




  1. Using a SimpleCursorAdapter to list all products, querying product_tag in the ViewBinder for each row, and programmatically creating views for each tag and inserting them into the row.




  2. Querying products LEFT JOIN product_tag ON products.id = product_tag.product_id, resulting in one row per tag (or one row per product with no tags) and manually populating the list. This seems more complicated and hacky, and duplicates some data, but avoids querying the db for each row in products.




I'd love it if someone more experienced with Android could comment on the most efficient way to accomplish this! Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire