【问题标题】:Issues with a MatrixCursorMatrixCursor 的问题
【发布时间】:2012-01-27 05:46:01
【问题描述】:

如果联系人有多个号码,我必须为同一联系人创建一个包含多行的ListView。因此,具有 3 个号码的联系人将在 ListView 中显示为 3 个单独的行。为此,我创建了一个 MatrixCursor 用于添加单独的行,然后将此光标添加到我的 ListView 中。

    private static final String[] arr = { "_id", "name", "type_int", "value" };
    final String[] projection = new String[] { Phone.NUMBER, Phone.TYPE, };
    add_cursor = new MatrixCursor(arr);
    String[] values = { "", "", "", "" };

    Cursor cursor = argcontext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, buffer == null ? null : buffer.toString(), args, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

    if (cursor != null && cursor.moveToFirst()) {
        int i = 0;
        do {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            cursor.getCount());
            Cursor phone = argcontext.getContentResolver().query(Phone.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[] { contactId }, null);
            if (phone != null && phone.moveToFirst()) {
                do {
                    number = phone.getString(phone.getColumnIndex(Phone.NUMBER));
                    final int type = phone.getInt(phone.getColumnIndex(Phone.TYPE));
                    values[0] = String.valueOf(i);
                    values[1] = String.valueOf(name);
                    values[2] = String.valueOf(type);
                    values[3] = String.valueOf(number);
                    add_cursor.addRow(values);
                    i++;
                } while (phone.moveToNext());
                phone.close();
            }
        }
    }

它运行良好,但我的问题是当后台数据发生更改时,我需要再次调用此代码,但首先我需要清除此游标值或删除其中的所有行,然后继续添加新行我不知道该怎么做。没有:

cursor.removeRow()

函数,所以如果联系人数据发生变化,我必须再次调用此函数并且如果我继续创建:

new MatrixCursor(); 

对于每个requery,它都会为应用程序创建大量内存占用。这会导致应用程序崩溃,比如说如果有 3000 个联系人,那么这个游标需要大量内存,这会导致应用程序崩溃。有没有其他方法可以显示这种列表?

【问题讨论】:

    标签: android listview android-contacts matrixcursor


    【解决方案1】:

    来自documentaion,mCursor.close();关闭光标,释放其所有资源并使其完全无效。您必须重新分配内存,使用 new 再次使用它。这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 2010-12-25
      • 2014-04-05
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多