ListView using two cursoradapters?

て烟熏妆下的殇ゞ 提交于 2019-11-27 01:37:49

问题


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?


回答1:


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




回答2:


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);

}



回答3:


You can also use cwac-merge.

cwac-merge: Provides the MergeAdapter, a ListAdapter that blends multiple Views or ListAdapters into a single ListAdapter. Use this for section headings, blending multiple sources of data together, etc.




回答4:


Check out MatrixCursor.

Maybe this will help you also Android - Database Table Join



来源:https://stackoverflow.com/questions/3235746/listview-using-two-cursoradapters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!