2

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.

BST Kaal
  • 2,993
  • 6
  • 34
  • 52
  • I would query both ContentProviders and use a CursorJoiner. – njzk2 Jul 07 '14 at 16:32
  • @njzk2, thnx..., let me read about `CursorJoiner` coz i have heard this term for the first time. – BST Kaal Jul 07 '14 at 16:36
  • @njzk2 , is it only applicable on two cursors with their single coulmns??? – BST Kaal Jul 07 '14 at 16:48
  • No, why would you say that? You only need to pass the names of the columns to compare, the CursorJoiner does not worry about the other columns. (as you then need to query to original cursors in the iteration on the results.) – njzk2 Jul 07 '14 at 16:51
  • Can u refer me any link or tutorial...m not completely getting it. – BST Kaal Jul 07 '14 at 16:53
  • you basically create 2 cursors from your content providers, then create a CursorJoiner from that, then iterate on it, testing is the `result` object is left, right, or both. There is nothing to it. – njzk2 Jul 07 '14 at 16:55
  • In my first Cursor, i have **id**, the second cursor have suppose **names** & **phone_numbers** and the **id**. I need to show names and numbers in a list, but only those whose id match the id of my cursor. – BST Kaal Jul 07 '14 at 16:55
  • 1
    then the result you are interested in is `BOTH`. – njzk2 Jul 07 '14 at 16:59
  • Thnx @njzk2, its working (filtering) but not exactly the same i wanted, may be my implementation may have some issues. But the Answer of this question is exactly fulfilled. Write it as an answer. – BST Kaal Jul 07 '14 at 18:30

1 Answers1

2

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.

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • Yes...It worked...Thnx alot. :D, i was missing a key point, that both of the columns must be sorted in the same manner. That's why i was getting less filtration. – BST Kaal Jul 07 '14 at 19:03
  • Hello @njzk2, can u plz take a look at http://stackoverflow.com/questions/24863038/avoid-swiperefresh-while-fastscrolling-in-android plz, i m stuck in the middle of app – BST Kaal Jul 23 '14 at 09:14