Android - how to delete item from a cursor?

前端 未结 2 1220
长情又很酷
长情又很酷 2021-01-14 05:08

Let\'s say I make the following cursor to get the call log of someone:

String[] strFields = {
    android.provider.CallLog.Calls.NUMBER, 
    android.provide         


        
相关标签:
2条回答
  • 2021-01-14 05:11

    You can to a trick with a MatrixCursor. With this strategy, you copy the cursor, and leave out the one row you want to exclude. This is - obviously, not very efficient for large cursors as you will keep the entire dataset in memory.

    You also have to repeat the String array of column names in the constructor of the MatrixCursor. You should keep this as a Constant.

       //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());
            }
    

    I constantly battle with this; trying to make my apps with cursors and content providers only, keeping away from object mapping as long as I can. You should see some of my ViewBinders ... :-)

    0 讨论(0)
  • 2021-01-14 05:14

    Sorry mate you can't delete from a cursor.

    You must either use your ContentResolver or a SQL call of some sort..

    0 讨论(0)
提交回复
热议问题