如果联系人有多个号码,我必须为同一联系人创建一个具有多行的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个联系人,则此光标需要大量内存,这会导致应用程序崩溃。有没有其他方式来显示这种列表?
发布于 2015-10-29 20:08:32
在documentaion中,mCursor.close();关闭游标,释放游标的所有资源并使其完全无效。您必须重新分配内存,使用new再次使用它。这对我很管用。
https://stackoverflow.com/questions/8623285
复制相似问题