Search code examples
androidandroid-contentproviderandroid-contentresolver

How to JOIN (literaly) two/more ContentProviders in Android?


I have a ContentProvider with a DataBase having two columns named "_id" and "_name". First, i am copying the id and name from ContactsContract.CommonDataKinds.Phone. Then i need to change the data in my ContentProvider. Then i need to access id and name again from ContactsContract.CommonDataKinds.Phone but on the basis of those id (s) which are now in my **ContentProvider**.

I know how to use Join in two tables. But don't know how can i do this using ContentProviders.


Solution

  • Here is how I would do that:

    Cursor locals = // Get from my local ContentProvider
    Cursor distant = // Get from distant phone ContentProvider
    List<String> names = new ArrayList<>();
    CursorJoiner joiner = new CursorJoiner(locals, new String[] {"_id"}, distant, new String[] {"_id"});
    for (CursorJointer.Result joinerResult : joiner) {
        switch (joinerResult) {
        case BOTH:
            String name = distant.getString(distant.getColumnIndex("name"));
            names.add(name);
            break;
        }
    }
    

    names contain your list of names.