Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView using two cursoradapters?

I have some code which executes two queries against a database and returns two cursor objects. Is there any way that I can combine these two cursors so that all the ListView gets the data from both?

like image 656
Martyn Avatar asked Jul 13 '10 09:07

Martyn


2 Answers

FYI - An example of using MergeCursor()

c = Cursor containing Contacts columns from Contacts.CONTENT_URI

private Cursor mergeCursorSubset(Cursor c) {

    int userMobile = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
        workMobile = ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;

    String storedNumber = ContactsContract.CommonDataKinds.Phone.NUMBER,
            displayName =ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            numberType = ContactsContract.CommonDataKinds.Phone.TYPE,
            contactKey = ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
            whereClausePre = contactKey+" = '",
            whereClausePost = "AND ("+numberType+" = '"+userMobile+"' OR "+numberType+" = '"+workMobile+"'";


    Uri lookupUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;;

    Cursor [] m = new Cursor[c.getCount()]; 

    if (c.moveToFirst())
        for (int k = 0; k<c.getCount();k++){            
            //Find the mobile numbers
            Cursor u = this.getContentResolver().query(lookupUri,
                                                    new String[]{displayName, storedNumber, numberType}
                                                    , whereClausePre+c.getString(c.getColumnIndex(Contacts.LOOKUP_KEY))+"') " 
                                                        + whereClausePost, null, null);
            int i = 0;
            if (u.moveToFirst())
                m[i++] = u;

        } //for Each key

    return new MergeCursor(m);

}
like image 25
TheSolarSheriff Avatar answered Sep 24 '22 10:09

TheSolarSheriff


There's MergeCursor for that (if there's no way to join tables).

like image 107
yanchenko Avatar answered Sep 23 '22 10:09

yanchenko