ListView使用两个cursoradapters?

时间:2010-07-13 09:23:03

标签: android android-listview android-cursoradapter

我有一些代码对数据库执行两次查询并返回两个游标对象。有没有办法可以将这两个游标组合在一起,以便所有ListView都能从两个游标中获取数据?

4 个答案:

答案 0 :(得分:14)

那里有MergeCursor(如果无法连接表格)。

答案 1 :(得分:9)

仅供参考 - 使用MergeCursor()

的示例  c =包含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);

}

答案 2 :(得分:4)

您也可以使用cwac-merge

  

cwac-merge:提供MergeAdapter,   一个混合多个的ListAdapter   将Views或ListAdapter合并为一个   ListAdapter。用于此部分   标题,混合多种来源   数据等等。

答案 3 :(得分:1)

查看MatrixCursor

也许这对Android - Database Table Join

也有帮助