mardi 4 août 2015

Can I use Cursor in RecyclerView.Adapter for binding Cartesian product (two joined tables)?

Maybe I didn't quite understand the purpose and possibilities of the Cursor. I am interested in the issue.

I have two tables, and one-to-many relationship

Content
------------------
id
article_id
index
type
text
images

Images
------------------
id 
content_id
content_index
image_index
url
title

So, I want see all content for article with id = 1.

SELECT * FROM content
WHERE article_id = 1

Result rows:

id    article_id    index  type     text           images 
-------------------------------------------------------------
1     1             0      "text"   "Text1"        NULL
2     1             1      "text"   "Text2"        NULL
3     1             2      "text"   "Text3"        NULL

Ok, next in my CursorRecyclerViewAdapter I get cursor for each row in result and bind it to UI:

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {    
             String type = cursor.getString(TYPE);
             String text = cursor.getString(TEXT);
             //......
    }

But if content has type "images", I can use JOIN for joined tables and get result Cartesian product, something like:

   id    article_id    index  type       text       url            title
   -------------------------------------------------------------------------
   1     1             0      "text"     "Text1"    NULL           NULL  
   2     1             1      "text"     "Text2"    NULL           NULL
   3     1             2      "images"   NULL       "image1.png"   "img1"
   3     1             2      "images"   NULL       "image2.png"   "img2"
   3     1             2      "images"   NULL       "image3.png"   "img3"
   4     1             3      "text"     "Text3"    NULL           NULL

In this case how use cursor in

onBindViewHolder(ViewHolder viewHolder, Cursor cursor);        

There are typical solutions for this issue? I was looking for a solution to the StackOverflow, but with no results, I begin to doubt that in such cases, you can use the Cursor?

Aucun commentaire:

Enregistrer un commentaire