Content Provider with List and Many-to-Many Relationships

I have created a content provider for my application, I am using it in my main activity with CursorLoader and list. Several items are displayed in the list. An element can be attached to many tags.

I have defined 3 tables: Element (_id, description), Tag (_id, description), ElementTag (idElement, idTag).

In the request method of the content provider, I make my connection, but since the element can have many tags. I am getting something like in my list (which is also the result of my query):

  • Element1 | Tag1
  • Element1 | Tag2
  • Element1 | Tab3

But I would like:

  • Element 1 Tag1 Tag2 Tag3 (first cell)

Although I am not going to combine elements and tags, but in this case, I have to query for tags for each element in the cursor adapter (which is bad I think).

I also read a thread that might be denormalization is a better idea (make a column in the Element table containing the element tags separated by commas). But it will be slow if I want to get all the elements containing the tag.

Maybe I need to discard all CursorLoader work and just make one big hashmap thanks to the join request?

I really appreciate your thoughts.

Thank you in advance :)

+3


source to share


1 answer


If it can help, here's my workaround:

I am not making any join between the ELEMENT and TAG tables in the content provider. I only load items, so I only have one row for each item in the list view. I am implementing loadContent method in CursorAdapter, I am loading tags and tag / item relationship tables into two hash files.

This method is called in the CursorAdapter constructor and when onContentChanged () is called (this method is called thanks to the CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER flag).



Then, in the bindView method for CursorAdapter, when processing the current cursor item, I get all the tags bound to the item thanks to the two hash maps.

If anyone is interested, I can provide some code.

0


source







All Articles