【问题标题】:ContactsContract and performance联系方式合同和业绩
【发布时间】:2011-02-15 10:36:24
【问题描述】:

我想将所有联系电话放入列表视图。有些联系人有多个电话号码,我想显示一个列表:

Pippo
Number : +393934578987
Pippo
Number : +394578952364
Topolino
Number : +45124578972
Minnie
Number : +39454545445b
Minnie
Number : +39457879758
etc

我使用了带有 List 和 Maps 的 SimpleAdapter,但性能很差......我决定使用 SimpleCursorAdapter 并使用此代码访问表 ContactsContract 和 ContactsContract.CommonDataKinds.Phone 一个 CursorJoiner

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1", null, null);
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + Contacts._ID, null, null);
        String[] cursor_join = new String[]{
                Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER
        };
        MatrixCursor matrix_cursor = new MatrixCursor(cursor_join);
        CursorJoiner cursor_j = new CursorJoiner(cursor, new String[]{Contacts._ID}, phones, new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID});
        for (CursorJoiner.Result joinerResult : cursor_j) {
            String[] values;
            switch (joinerResult) {         
                case LEFT:             // handle case where a row in cursorA is unique             
                    values = new String[]{
                            cursor.getString(cursor.getColumnIndex(Contacts._ID)),
                            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)),
                            phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                        };
                        matrix_cursor.addRow(values);
                    break;         
                case RIGHT:             // handle case where a row in cursorB is unique             
                    break;         
                case BOTH:             // handle case where a row with the same key is in both cursors             
                    values = new String[]{
                            cursor.getString(cursor.getColumnIndex(Contacts._ID)),
                            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)),
                            phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                        };
                        matrix_cursor.addRow(values);
                    break;     
            }
        }
        while(matrix_cursor.moveToNext()){
            contactId = matrix_cursor.getInt(matrix_cursor.getColumnIndex(Contacts._ID));
            String name = matrix_cursor.getString(matrix_cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String num = matrix_cursor.getString(matrix_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }

但它不起作用

有人告诉我为什么吗?

谢谢

【问题讨论】:

    标签: android listview android-listview contacts


    【解决方案1】:

    这些天我终于解决了这个问题。我放了一些东西以防 LEFT 移动光标并在左光标超出范围时打破 RIGHT(实际上是isAfterLast()

    case LEFT:
    rightcursor.moveToPrevious();
    ......
    rightcursor.moveToNext();
    break;
    
    case RIGHT:
    if(leftcursor.isAfterLast()){break;}
    ....
    

    不确定这是否有帮助以及您遇到了什么问题。

    【讨论】:

      【解决方案2】:

      也许您错过了对matrix_cursor.moveToFirst() 的呼叫。

      【讨论】:

        猜你喜欢
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 1970-01-01
        • 2012-02-05
        • 2015-08-31
        • 1970-01-01
        相关资源
        最近更新 更多