Update

public class UbahDataActivity extends AppCompatActivity implements View.OnClickListener {
    private DBSource dataSource;

    private long id;
    private String harga;
    private String merk;
    private String nama;

    private EditText edNama;
    private EditText edHarga;
    private EditText edMerk;

    private TextView txId;

    private Button btnSave;
    private Button btnCancel;

    private Barang barang;

    public UbahDataActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ubah_data);

        edNama = (EditText) findViewById(R.id.editText_nama);
        edHarga = (EditText) findViewById(R.id.editText_harga);
        edMerk = (EditText) findViewById(R.id.editText_merk);
        txId = (TextView) findViewById(R.id.text_id_barang);

        //buat sambungan baru ke database
        dataSource = new DBSource(this);
        dataSource.open();
        
        // ambil data barang dari extras
        Bundle bun = this.getIntent().getExtras();
        id = bun.getLong("id");
        harga = bun.getString("harga");
        merk = bun.getString("merk");
        nama = bun.getString("nama");
        //masukkan data-data barang tersebut ke field editor
        txId.append(String.valueOf(id));
        edNama.setText(nama);
        edHarga.setText(harga);
        edMerk.setText(merk);

        //set listener pada tombol
        btnSave = (Button) findViewById(R.id.button_save_update);
        btnSave.setOnClickListener(this);
        btnCancel = (Button) findViewById(R.id.button_cancel_update);
        btnCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            // apabila tombol save diklik (update barang)
            case R.id.button_save_update:
                barang = new Barang();
                barang.setHarga_barang(edHarga.getText().toString());
                barang.setNama_barang(edNama.getText().toString());
                barang.setMerk_barang(edMerk.getText().toString());
                barang.setId(id);
                dataSource.updateBarang(barang);

                Intent i = new Intent(this, TampilDataActivity.class);
                startActivity(i);

                UbahDataActivity.this.finish();

                dataSource.close();
                break;

            case R.id.button_cancel_update:

                finish();
                dataSource.close();
                break;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/text_id_barang"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:text="Ubah Data"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText_nama"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:ems="10">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_merk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editText_harga"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText_merk"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:inputType="number" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText_harga"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="1">

        <Button
            android:id="@+id/button_save_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Edit" />

        <Button
            android:id="@+id/button_cancel_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"

            android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

public Barang getBarang(long id) {
    Barang barang = new Barang();

    Cursor cursor = database.query(DBHelper.TABLE_NAME, allColumns,
            "_id =" + id, null, null, null, null);
    cursor.moveToFirst();
    barang = cursorToBarang(cursor);
    cursor.close();

    return barang;
}


public void updateBarang(Barang b) {
    String strFilter = "_id=" + b.getId();

    ContentValues args = new ContentValues();
    args.put(DBHelper.COLUMN_NAME, b.getNama_barang());
    args.put(DBHelper.COLUMN_MERK, b.getMerk_barang());
    args.put(DBHelper.COLUMN_HARGA, b.getHarga_barang());

    database.update(DBHelper.TABLE_NAME, args, strFilter, null);
}

dialog_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_edit_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit" />

    <Button
        android:id="@+id/button_delete_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete" />
</LinearLayout>

Last updated

Was this helpful?