How does "selection" and "selectionArgs" clauses work for ContentProvider

173 views Asked by At

I have a custom class with implementation:

private static class Item {
    String userID;
    String userEmail;

    Item(String userID, String userEmail) {
        this.userID = userID;
        this.userEmail = userEmail;
    }
}

Here's how I update values in ContentProvider:

@Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    int itemPosition = 0;

    // Adding dummy data incase Items List is empty.
    if (items.isEmpty()) {
        add(values);
    } else {
        items.set(itemPosition, new Item(values.getAsString(K.USER_ID_COLUMN_NAME), values.getAsString(K.USER_EMAIL_COLUMN_NAME)));
    }
    return notifyChange(uri, itemPosition);
}

And here's how I pass values to the method:

ContentValues values = new ContentValues();
values.put(K.USER_ID_COLUMN_NAME, "1234");
values.put(K.USER_EMAIL_COLUMN_NAME, "[email protected]);

getContext().getContentResolver().update(K.CONTENT_URI, values, null, null);

My question is, how do I refactor the update method so that I can make use of selection and selectionArgs to update only one of the columns and leave the other columns as is? I don't want to complicate things with SQLite but I'd rather go with MatrixCursor.

0

There are 0 answers