MatrixCursor的问题

时间:2011-12-24 07:16:12

标签: 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(); 

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

1 个答案:

答案 0 :(得分:0)

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