Adding rows into Cursor manually

前端 未结 4 558
無奈伤痛
無奈伤痛 2020-12-03 13:47

I have an array of phone numbers and I want to get the corresponding contact names from the contacts database.

In the array of phone numbers, I also have some number

4条回答
  •  醉酒成梦
    2020-12-03 14:16

    Easiest way to add rows in a cursor is to use a MatrixCursor and a MergeCursor. Those two classes are from the SDK and here to solve that kind of problems.

    Basically what you do is :

    1. Put the rows you want to add in a MatrixCusror
    2. Merge your cursor and your matrixCursor using a MergeCursor

    Something like:

    // Create a MatrixCursor filled with the rows you want to add.
    MatrixCursor matrixCursor = new MatrixCursor(new String[] { colName1, colName2 });
    matrixCursor.addRow(new Object[] { value1, value2 });
    
    // Merge your existing cursor with the matrixCursor you created.
    MergeCursor mergeCursor = new MergeCursor(new Cursor[] { matrixCursor, cursor });
    
    // Use your the mergeCursor as you would use your cursor.
    

提交回复
热议问题