Android Tutorial CRUD SQLite DB (Read DB)
Bismillah. Assalamualaikum sobat zainsoft. Kembali kita lanjutkan tutorial Android Java tentang SQLite. Tulisan ini merupakan lanjutan dari Android Tutorial CRUD SQLite DB (Create DB). Setelah kita berhasil menciptakan database sqlite di android, kali ini kita akan mencoba untuk memasukkan data dan menampilkannya. Jadi untuk skripnya kita lanjutkan dari source code yang sebelumnya ya sob.
Untuk selanjutnya kita akan buat satu Activity dengan nama AccountActivity. Activity ini berfungsi untuk meng-entry data jika database kosong. Sekarang kita fokus untuk mendesain tampilan activity_account.xml.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AccountActivity">
<Button
android:id="@+id/button_simpan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/simpan"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_keterangan"
app:layout_constraintVertical_bias="0.053" />
<TextView
android:id="@+id/title_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="50dp"
android:fontFamily="serif"
android:text="@string/detail_account"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/title_account_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/title_nama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_account_id">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_nama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/nama_account"
android:nextFocusDown="@id/input_alamat" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/title_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_nama">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/alamat_account"
android:nextFocusDown="@id/input_noponsel" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/title_noponsel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_alamat">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_noponsel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/no_ponsel_account"
android:nextFocusDown="@id/input_keterangan" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/title_keterangan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_noponsel">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_keterangan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/keterangan"
android:nextFocusDown="@id/button_simpan" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/title_account_id"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginBottom="599dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_activity">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input_account_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_id"
android:inputType="number"
android:nextFocusDown="@id/input_nama" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>Setelah kita desain xml nya, sekarang kita berikan sebuah Model untuk pengambilan data dari SQLite DB. Kita beri nama ModelAccount.java. Mohon diperharikan implements Serializable, dan public static String... inilah bertugas untuk membagikan data agar data dapat ditampilkan dari kelas yang lain.
public class ModelAccount implements Serializable {
//kita setting public static agar data dapat langsung digunakan dari kelas yang lain
public static String account_id,nama,alamat,no_ponsel,keterangan;
public ModelAccount() {
}
public static String getAccount_id() {
return account_id;
}
public static void setAccount_id(String account_id) {
ModelAccount.account_id = account_id;
}
public static String getNama() {
return nama;
}
public static void setNama(String nama) {
ModelAccount.nama = nama;
}
public static String getAlamat() {
return alamat;
}
public static void setAlamat(String alamat) {
ModelAccount.alamat = alamat;
}
public static String getNo_ponsel() {
return no_ponsel;
}
public static void setNo_ponsel(String no_ponsel) {
ModelAccount.no_ponsel = no_ponsel;
}
public static String getKeterangan() {
return keterangan;
}
public static void setKeterangan(String keterangan) {
ModelAccount.keterangan = keterangan;
}
}Nah, untuk selanjutnya kita akan fokus ke kelas DBHelper.java, untuk menuliskan skrip query untuk insert dan menampilkan data account.
public class DBHelper extends SQLiteOpenHelper {
private static String DB_NAME = "zainsoft.db";
private static Integer DB_VERSION = 2;
private final Context context;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE account(" +
"account_id char(20) primary key," +
"nama char(20)," +
"alamat char(255)," +
"no_ponsel char(20)," +
"keterangan char(250))"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS account");
onCreate(db);
}
public long insertAccount(String account_id, String nama, String alamat, String noponsel, String keterangan){
SQLiteDatabase db = this.getWritableDatabase();
// Creating content values
ContentValues values = new ContentValues();
values.put("account_id",account_id);
values.put("nama",nama);
values.put("alamat",alamat);
values.put("no_ponsel",noponsel);
values.put("keterangan",keterangan);
long insert = db.insert("account", null, values);
Toast.makeText(context, "Entry Data Sukses", Toast.LENGTH_LONG).show();
return insert;
}
public Cursor getDataAccount(){
String selectQueryAccount = "select * from account";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c_account = db.rawQuery(selectQueryAccount,null);
System.out.println("Count: "+c_account.getCount());
if (c_account.getCount()>0) {AccountActivity.table_kosong = false;
if (c_account.moveToFirst()) {
do {
ModelAccount modelAccount = new ModelAccount();
modelAccount.setAccount_id(c_account.getString(c_account.getColumnIndex("account_id")));
modelAccount.setNama(c_account.getString(c_account.getColumnIndex("nama")));
modelAccount.setAlamat(c_account.getString(c_account.getColumnIndex("alamat")));
modelAccount.setNo_ponsel(c_account.getString(c_account.getColumnIndex("no_ponsel")));
modelAccount.setKeterangan(c_account.getString(c_account.getColumnIndex("keterangan")));
} while (c_account.moveToNext());
}
}
else {
AccountActivity.table_kosong = true;
}
return c_account;
}
}Oke, sekarang kita sudah mendekati bagian akhir penulisan skrip pada kelas AccountAcitivity.java. public class AccountActivity extends AppCompatActivity {
DBHelper dbHelper;
Button buttonSimpan;
ModelAccount modelAccount = new ModelAccount();
TextInputEditText accountId,nama,alamat,noponsel,keterangan;
public static boolean table_kosong = false;
String status_entry = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
dbHelper = new DBHelper(this);
buttonSimpan = findViewById(R.id.button_simpan);
accountId = findViewById(R.id.input_account_id);
nama = findViewById(R.id.input_nama);
alamat = findViewById(R.id.input_alamat);
noponsel = findViewById(R.id.input_noponsel);
keterangan = findViewById(R.id.input_keterangan);
buttonSimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (
TextUtils.isEmpty(accountId.getText().toString()) ||
TextUtils.isEmpty(nama.getText().toString()) ||
TextUtils.isEmpty(alamat.getText().toString()) ||
TextUtils.isEmpty(noponsel.getText().toString()) ||
TextUtils.isEmpty(keterangan.getText().toString())){
Toast.makeText(AccountActivity.this, "Mohon cek kembali\nData tidak boleh kosong", Toast.LENGTH_SHORT).show();
}
else{
if (status_entry.equalsIgnoreCase("add")) {
dbHelper.insertAccount(accountId.getText().toString(), nama.getText().toString(), alamat.getText().toString(), noponsel.getText().toString(), keterangan.getText().toString());
onBackPressed();
}
else if (status_entry.equalsIgnoreCase("edit")){
dbHelper.updateAccount(modelAccount.getAccount_id(),accountId.getText().toString(), nama.getText().toString(), alamat.getText().toString(), noponsel.getText().toString(), keterangan.getText().toString());
onBackPressed();
}
}
}
});
if (table_kosong == true){
accountId.requestFocus();
status_entry = "add";
}
else if (table_kosong == false){
dbHelper.getDataAccount();
accountId.setText(modelAccount.getAccount_id());
nama.setText(modelAccount.getNama());
alamat.setText(modelAccount.getAlamat());
noponsel.setText(modelAccount.getNo_ponsel());
keterangan.setText(modelAccount.getKeterangan());
status_entry = "edit";
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}Categories: Database
Sterling
References: Pay id pokies australia https://khargharonline.com
Klaudia
References: Free chips neosurf casino australia neosurf casino minimum deposit australia
Junko
References: Neosurf casino free chip australia doc.anagora.org
Matt
References: Neosurf casino sites for australian players neosurf safe casino australia
Gloria
References: Same day payout pokies payid best pay id pokies
Ulrike
Our experts have done extensive and elaborate research into determining the highest-ranked online casinos for Australian players that feature PayID payments. Begin by selecting Australian online pokies PayID casinos that prioritise fair gameplay and provide a secure environment. To initiate the use of the pokies PayID method, you’ll need to first establish an account with an Aussie bank and attach an identifier. Instead of sharing your card/BSB number and other details, simply enter a unique code linked to your bank account. Options like POLi, BPAY, and Cryptocurrencies have gained popularity among Aussie players for their reliability and convenience. Sometimes, you have to activate appropriate PayID pokies Australia no deposit bonus codes. References: https://climategains.community/
Mellissa
References: Neosurf voucher casino australia https://concretewiki.site
Barbara
References: $1 minimum deposit casino neosurf australia is.gd
Veola
References: Casinos accepting payid https://hedgedoc.ludos-disciplinarum-misi.fyi/
Callum
References: Payid instant withdrawal pokies http://www.okaywan.com/home.php?mod=space&uid=867052
Lasonya
References: Payid casino without verification https://sciencemission.com/
Sheila
References: Payid withdrawal casino australia https://pbase.com
Ulysses
References: Casino online payid australia https://hedgedoc.ludos-disciplinarum-misi.fyi/u3c27nn5TKChCslv1312gw
Taylor
References: Casino deposit with payid bridgedesign.site
Alethea
References: Payid gambling sites https://codimd.camba.coop/oW3o9OguS1CZftp-mHWQng
Sofia
References: Top 10 payid casinos australia https://pad.deckenpfronn.info/cFjZ8r8ISOaVgfR008nWMA
Tarah
References: Fast payout casino payid http://oneclickcarehk.com/forum/home.php?mod=space&uid=324100
Beverly
References: Safe payid casinos http://creativespace.biz/
Anderson
References: Casinos ios bizum https://rdcvw.com/?545113
Berenice
References: Quick payout payid casino https://www.play56.net/home.php?mod=space&uid=6740847
Tia
References: Descargar app casino bizum http://xuetao365.com/home.php?mod=space&uid=745455
Thaddeus
References: $20 payid deposit casino https://mapleprimes.com/users/puffintwine3
Riley
References: Payid withdrawal casino australia carwiki.site
Ona
References: Payid slot games http://bbs.abcdv.net/
Johnathan
References: Safe payid casinos md.sigma2.no
Iesha
References: Casinos that take payid http://www.lezcc.com/?785399
Valorie
References: Ile trwa wyplata z kasyna na karte kredytowa https://schoolido.lu/user/chessgrill60/
Peggy
References: Casino online payid australia https://gitlab.keysmith.bz/scentmaria51
Lorri
References: Payid slot sites https://app.wedonthavetime.org/profile/brushchance06
Julieta
References: Same day payout casino payid https://forum.xingsi.org/home.php?mod=space&uid=1872692
Ismael
References: Top 10 payid casinos australia https://valdez-warming-2.federatedjournals.com
Robin
References: Australian casinos that accept payid https://pbase.com/rangefired62/
Marietta
References: Online pokies payid https://pad.batsense.net
Felipe
References: Best payid casino australia onespotsocial.com
Leave a comment