Pada kesempatan kali ini saya akan melanjutkan tutorial tentang cara membuat aplikasi CRUD dengan Android Studio (Java). Di-part 4 ini saya akan memberikan cara update data di aplikasi CRUD.
Tahap Pembuatan
-
Untuk langkah pertamanya silakan dibuka kembali project yang sudah dibuatnya.
-
Setelah itu tambahkan method berikut di dalam class "DBDataSource" yang berada di package controller.
public void updateData(long bookId, String bookName, String bookAuthor) { ContentValues values = new ContentValues(); values.put(DBHelper.columnName, bookName); values.put(DBHelper.columnAuthor, bookAuthor); String[] args = {"" + bookId}; database.update(DBHelper.tableName, values, DBHelper.columnId + "=?", args); }
-
Kemudian buat package update di dalam package view.
-
Setelah itu buat class "ListUpdate" di dalam package update, dengan isian berikut.
package com.herdaynote.simplecrud.view.update; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import com.herdaynote.simplecrud.R; import com.herdaynote.simplecrud.controller.DBDataSource; import com.herdaynote.simplecrud.model.Book; import java.util.ArrayList; public class ListUpdate extends Activity { private DBDataSource dataSource; private ArrayList<Book> values; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_update); dataSource = new DBDataSource(this); dataSource.open(); values = dataSource.readData(); ArrayAdapter<Book> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, values ); listView = findViewById(R.id.listBook); listView.setAdapter(adapter); listView.setOnItemClickListener( (parent, v, position, id) -> { Book book = values.get(position); Intent intent = new Intent(ListUpdate.this, UpdateData.class); intent.putExtra("id", book.getBookId()); intent.putExtra("name", book.getBookName()); intent.putExtra("author", book.getBookAuthor()); startActivity(intent); finish(); } ); } }
-
Buat juga class "UpdateData" di dalam package update, dengan isian berikut.
package com.herdaynote.simplecrud.view.update; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.herdaynote.simplecrud.R; import com.herdaynote.simplecrud.controller.DBDataSource; public class UpdateData extends Activity implements OnClickListener { private EditText edtBookName, edtBookAuthor; private Button btnUpdate; private DBDataSource dataSource; private long bookId; private String bookName, bookAuthor; @SuppressLint("SetTextI18n") @Override protected void onCreate(Bundle savedinstanceState) { super.onCreate(savedinstanceState); setContentView(R.layout.update_data); bookId = getIntent().getExtras().getLong("id"); bookName = getIntent().getExtras().getString("name"); bookAuthor = getIntent().getExtras().getString("author"); edtBookName = (EditText) findViewById(R.id.edtBookName); edtBookName.setText(bookName); edtBookAuthor = (EditText) findViewById(R.id.edtBookAuthor); edtBookAuthor.setText(bookAuthor); btnUpdate = (Button) findViewById(R.id.btnUpdate); btnUpdate.setOnClickListener(this); dataSource = new DBDataSource(this); dataSource.open(); } @SuppressLint("NonConstantResourceId") public void onClick(View v) { try { bookName = edtBookName.getText().toString(); bookAuthor = edtBookAuthor.getText().toString(); if (v.getId() == R.id.btnUpdate) { dataSource.updateData(bookId, bookName, bookAuthor); Toast.makeText(this, "Data updated!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this, ListUpdate.class); startActivity(intent); finish(); } } catch (Exception e) { Toast.makeText(this, "Please fill in the data!", Toast.LENGTH_SHORT).show(); } } }
-
Setelah itu buat layout baru "list_update.xml", dengan isian berikut.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context=".view.update.ListUpdate"> <TextView android:id="@+id/title_app" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/updateText" android:gravity="center_horizontal" android:textSize="42sp" android:layout_margin="20dp"/> <ListView android:id="@+id/listBook" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/title_app"/> </RelativeLayout>
-
Buat juga layout baru "update_data.xml", dengan isian berikut.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context=".view.update.UpdateData"> <TextView android:id="@+id/title_app" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/updateText" android:textSize="42sp" android:layout_marginTop="20dp" android:layout_marginBottom="15dp"/> <EditText android:id="@+id/edtBookName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/title_app" android:hint="@string/book_name" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:textSize="20sp" android:layout_marginBottom="10dp" android:inputType="text" android:importantForAutofill="no" /> <EditText android:id="@+id/edtBookAuthor" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/edtBookName" android:hint="@string/book_author" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:textSize="20sp" android:layout_marginBottom="10dp" android:inputType="text" android:importantForAutofill="no" /> <Button android:id="@+id/btnUpdate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/edtBookAuthor" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="@string/updateText" android:textSize="20sp" android:padding="10dp"/> </RelativeLayout>
-
Setelah itu ubah tag comment "// start update activity" yang ada di dalam class "MainActivity" yang berada di package view, dengan isian berikut.
Intent i3 = new Intent(this, ListUpdate.class); startActivity(i3); break;
-
Lalu tambahkan kode berikut di dalam tag "application" yang berada di "AndroidManifest.xml".
<activity android:name=".view.update.ListUpdate"/> <activity android:name=".view.update.UpdateData"/>