android - MatrixCursor 的问题

标签 android listview android-contacts matrixcursor

如果联系人有多个号码,我必须为同一个联系人创建一个包含多行的 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(); 

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

最佳答案

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

关于android - MatrixCursor 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8623285/

相关文章:

android - 应用未在适用于平板设备的 Google Play 商店中列出

android.graphics.drawable.Drawable.isProjected()' 在空对象引用上

java - 我想要两个 ListView 同时滚动

Jquery Mobile ListView 禁用画外音

android 检查联系人是否安装了 android 应用程序(如 whatsapp)

android - 我的小部件背景消失了 - android

尽管是 Collections.unmodifiableList(),但 Java List 从某处被修改

android - 将 ListView 包装在 LinearLayout 中

java - 在 Android 中使用 ContactsContract 检索电话号码 - 功能不起作用

java - 打开 LINE Android 应用程序与特定联系人的聊天屏幕