Android -如何从光标中删除项目



假设我使用以下光标来获取某人的通话记录:

String[] strFields = {
    android.provider.CallLog.Calls.NUMBER, 
    android.provider.CallLog.Calls.TYPE,
    android.provider.CallLog.Calls.CACHED_NAME,
    android.provider.CallLog.Calls.CACHED_NUMBER_TYPE
    };
String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
Cursor mCallCursor = getContentResolver().query(
        android.provider.CallLog.Calls.CONTENT_URI,
        strFields,
        null,
        null,
        strOrder
        );

现在我该如何删除游标中的第I项呢?这也可以是一个光标获取音乐列表,等等。那么我必须问,这可能吗?我可以理解第三方应用程序不允许删除某些游标。

谢谢。

对不起,您不能从光标中删除。

你必须使用你的ContentResolver或某种SQL调用…

您可以使用MatrixCursor来实现一个技巧。使用此策略,您可以复制游标,并省略要排除的一行。显然,对于大型游标来说,这不是很有效,因为您将把整个数据集保存在内存中。

您还必须在MatrixCursor的构造函数中重复列名的String数组。你应该保持这个常数。

   //TODO: put the value you want to exclude
   String exclueRef = "Some id to exclude for the new";
   MatrixCursor newCursor = new MatrixCursor(new String[] {"column A", "column B");
         if (cursor.moveToFirst()) {
            do {
                // skip the copy of this one .... 
                if (cursor.getString(0).equals(exclueRef))
                    continue;
                newCursor.addRow(new Object[]{cursor.getString(0), cursor.getString(1)});
            } while (cursor.moveToNext());
        }

我经常与这个作斗争;试图让我的应用程序只有游标和内容提供程序,尽可能远离对象映射。你应该看看我的viewbinder…: -)

最新更新