0

So, I have android application using Sqlite Database. I has a cardview to showing the data. In the table, I have field like, name(text), cost(int) and an image(BLOB). I want to update the data. So, when I change and update the name or the cost, the image not updated and return blank or null. But when I change and update the image, it successfully updated.

First, this is my select query to showing the name, cost and image in update class

public ModelProduk getnama(int selection){
    SQLiteDatabase db = this.getReadableDatabase();
    String whereclause = KEY_ID_PRODUK + "=?";
    String[] whereargs = new String[]{String.valueOf(selection)};
    Cursor cursor = db.query(
            TABLE_PRODUK,
            null,
            whereclause,
            whereargs,
            null,
            null,
            null
    );
    ModelProduk modelProduk = new ModelProduk();
    if (cursor.moveToFirst()) {

        modelProduk.set_id(Integer.parseInt(cursor.getString(0)));
        modelProduk.set_nama(cursor.getString(1));
        modelProduk.set_harga(Integer.parseInt(cursor.getString(2)));
        modelProduk.set_gambar(cursor.getBlob(3));
    }
    cursor.close();
    db.close();
    return modelProduk;
}

Here's my database to update

public void update(ModelProduk modelProduk) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAMA_PRODUK, modelProduk.get_nama());
    values.put(KEY_HARGA_PRODUK, modelProduk.get_harga());
    values.put(KEY_GAMBAR_PRODUK, modelProduk.get_gambar());

    String where = "id=?";
    String[] whereArgs = new String[] {String.valueOf(modelProduk.get_id())};
    // update baris
    db.update(TABLE_PRODUK, values, where, whereArgs);
    db.close();
}

my java class for update

private void init(){

    pilih = getIntent().getIntExtra("id_produk", 0);
    db = new Database(this);
    modelProduk = db.getnama(pilih);

    gambar_produku = (ImageView) findViewById(R.id.pilihgambaru);
    tambahgambaru = (Button) findViewById(R.id.btaddu);
    simpandatau = (Button) findViewById(R.id.btsimpanu);
    deskripsiu = (EditText) findViewById(R.id.etdesku);
    harga_produku = (EditText) findViewById(R.id.ethargau);
    kembaliu = (Button) findViewById(R.id.btkembaliu);

    deskripsiu.setText(modelProduk.get_nama());
    harga_produku.setText(String.valueOf(modelProduk.get_harga()));
    if (modelProduk.get_gambar() != null) {
        gambar_produku.setImageBitmap(bitmap(modelProduk.get_gambar()));
    }
}
public void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Pilih Gambar"), 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {


        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            gambar_produku.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


public byte[] getImageByte(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    byte imageInByte[]=null;
    if(bitmap!=null) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);
        imageInByte=stream.toByteArray();
    }
    return imageInByte;
}

public Bitmap bitmap (byte[] byteImage){
    byte[] outImage = byteImage;
    Bitmap image ;
    if (outImage != null){
        ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
        image = BitmapFactory.decodeStream(imageStream);
    }else {
        image= null;
    }
    return image;
}

my onclick to save the update

simpandatau.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            modelProduk.set_nama(deskripsiu.getText().toString());
            modelProduk.set_harga(Integer.parseInt(harga_produku.getText().toString()));
            modelProduk.set_gambar(getImageByte(bitmap));
            db.update(modelProduk);
            Toast.makeText(getApplicationContext(), "Data Berhasil Diubah!", Toast.LENGTH_SHORT).show();

        }
    });

Please Help! I have read many solutions but it's not same to my problems. Thank you!

1 Answer 1

1

You could try the following. This will attempt to get the row that is to be updated before updating. However, rather than return blank or null or update successful it returns an integer which can be -1, 0 or 1 which indicates three possible outcomes.

  • If the row doesn't exist then it will return -1 (NOTUPDATED).
  • Otherwise it will then compare the stored image against the image passed.
    • If the images are different then it will add the new image to values and set the value to be returned to 1 (IMAGEUPDATED).
    • Otherwise the new image is not added to the values and the value to be returned will remain as 0 (IMAGENOTUPDATED). By not adding the image to values the gambar column will not be included in the SET clause of the SQL generated by the update method.

:-

public int update(ModelProduk modelProduk) {

    final int NOTUPDATED = -1;
    final int IMAGENOTUPDATED = 0;
    final int IMAGEUPDATED = 1;

    String where = "id=?";
    String[] whereArgs = new String[] {String.valueOf(modelProduk.get_id())};
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    // get the current row from the database (return -1 if it doesn't exist)
    Cursor before_update = db.query(TABLE_PRODUK,null,where,whereArgs,null,null,null);
    if (!before_update.moveToFirst) {
        before_update.close();
        return NOTUPDATED;
    }
    // Default value to return
    rv = IMAGENOTUPDATED;
    // Compare the store image against the new image, if not the same
    //    then include the image in the update
    if (!Arrays.equals(before_update.getBlob(3),modelProduk.get_gambar)) {
        values.put(KEY_GAMBAR_PRODUK, modelProduk.get_gambar());
        rv = IMAGEUPDATED;
    }
    before_update.close();

    values.put(KEY_NAMA_PRODUK, modelProduk.get_nama());
    values.put(KEY_HARGA_PRODUK, modelProduk.get_harga());

    // Update baris checking to see if an update occurred, if not
    //    then set return value to -1 (could be a different status code)
    if (db.update(TABLE_PRODUK, values, where, whereArgs) = 0) {
        rv = NOTUPDATED;
    }
    db.close();
    before_update.close();
    return rv;
}
  • Note the above is in-principle code, it has not been tested and therefore may contain errors.

  • You may wish to define NOTUPDATED, IMAGENOTUPDATED and IMAGEUPDATED elsewhere with greater scope.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for helping me, but my image still not get updated.
is there any other suggestion??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.