Search code examples
sql-updateandroid-contentprovider

ContentUris.withAppendedId method in android content provider can i leave the "id" part of perameter blank?


for the following method

 Uri uri = ContentUris.withAppendedId((MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);

i have read many examples but none of the examples tell me what to do with "id" part of the perameter, i don't want to assign which row because that will change depending of what row the specific image is stored in. so i can't hard code "id". how to deal with this? and can i leave the id as null?

edit

i just found this a few minutes ago from a website, http://www.grokkingandroid.com/android-tutorial-using-content-providers/

     values.clear();
 values.put(Words.WORD, "Zaphod");
 Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id);
 long noUpdated = resolver.update(uri, values, null, null)

here is the quote from the author, "Since I changed only one record, a URI with an appended ID is sufficient. But if you want to update multiple values, you should use the normal URI and a selection clause."

so he says to use a "normal URI and selection clause" so how do i do that?

this is my selection statement:

     String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";

where "filename" is a string variable that varies depending on the image selected, for example, in this case /mnt/sdcard/pic09.png


Solution

  • I'm not sure if I understood your question, but regarding the part when you ask how do you do the normal URI and selection clause, the "trick" is that using content providers you dont put the whole selection in one argument as:

    String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
    

    but instead, you introduce the selection type, selection arguments through different input variables. So for example, the

     update where MediaStore.Images.Media.DATA equals filename 
    

    becomes:

    update (uri, contentValues, String selection, String[] selectionArgs)
    

    where uri points to the table (not the row) content values point to a ContentValues object with your new values selection must contain the fields to be compared + comparrisson type + question mark, as

    MediaStore.Images.Media.DATA + " = ?";
    

    And selection arguments is an array with all the strings replacing question marks on your selection string, so in your case just

    {filename}