【问题标题】:ListView using two cursoradapters?ListView 使用两个光标适配器?
【发布时间】:2010-07-13 09:23:02
【问题描述】:

我有一些代码对数据库执行两个查询并返回两个游标对象。有什么方法可以组合这两个游标,以便所有ListView 都从两者中获取数据?

【问题讨论】:

    标签: android android-listview android-cursoradapter


    【解决方案1】:

    MergeCursor (如果无法加入表格)。

    【讨论】:

    • 我认为有一种方法可以使用 SQLiteQueryBuilder setTables (String inTables) 方法来连接表,我自己会尝试一下
    【解决方案2】:

    仅供参考 - 使用 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);
    
    }
    

    【讨论】:

    • 你能进一步解释一下吗?
    【解决方案3】:

    您也可以使用cwac-merge

    cwac-merge:提供MergeAdapter, 一个混合多个的 ListAdapter 视图或 ListAdapters 合并为一个 列表适配器。将此用于部分 标题,混合多种来源 数据一起等。

    【讨论】:

      【解决方案4】:

      查看MatrixCursor

      也许这对你也有帮助 Android - Database Table Join

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-12
        • 1970-01-01
        • 1970-01-01
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多