Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

android - how to use join query in CursorLoader when its constructor does not support it

I am having two tables with 1:n relationship, I am using content provider and cursorloader.

How would I make a join query to work with cursor loader? I could hack it up somehow with rawSql inside content provider but how to do it in cursor loader constructor is beyond me.

Thanks a lot !

CursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

How will query for join when Uri could only point to one table

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Uri does not point to any table. It points to whatever you feel like pointing it to.

Let's pretend that your two tables are Customer and Order. One customer may have many orders. You want to execute a query to get all outstanding orders... but you want to join in some customer-related columns that you will need, such as the customer's name.

Let's further pretend that you already have content://your.authority.goes.here/customer and content://your.authority.goes.here/order defined to purely query those tables.

You have two choices:

  1. Add the join of the customer's display name on your /order Uri. Having another available column probably will not break any existing consumers of the provider (though testing is always a good idea). This is what ContactsContract does -- it joins in some base columns, like the contact's name, on pretty much all queries of all tables.

  2. Create content://your.authority.goes.here/orderWithCust that does the same basic query as /order does, but contains your join. In this case, you could have insert(), update(), and delete() throw some sort of RuntimeException, to remind you that you should not be modifying data using /orderWithCust as a Uri.

In the end, designing a ContentProvider Uri system is similar to designing a REST Web service's URL system. In both cases, the join has to be done on the provider/server side, and so you may need to break the one-table-to-one-URL baseline to offer up some useful joins.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...