CRUD di Android Studio
Halaman XML nya
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Data" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</LinearLayout>
Halaman MainActivitynya
package com.example.botton
import android.annotation.SuppressLint
import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.ListView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var editTextName: EditText
private lateinit var listView: ListView
private lateinit var database: SQLiteDatabase
private val dbHelper = object : SQLiteOpenHelper(this, "MyDatabase",
null, 1) {
override fun onCreate(db: SQLiteDatabase?) {
db?.execSQL("CREATE TABLE IF NOT EXISTS MyTable (name TEXT)")
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int,
newVersion: Int) {
db?.execSQL("DROP TABLE IF EXISTS MyTable")
onCreate(db)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editTextName = findViewById(R.id.editTextName)
listView = findViewById(R.id.listView)
val buttonAdd: Button = findViewById(R.id.buttonAdd)
buttonAdd.setOnClickListener {
val name = editTextName.text.toString()
insertData(name)
updateListView()
editTextName.text.clear()
}
database = dbHelper.writableDatabase
updateListView()
}
override fun onDestroy() {
super.onDestroy()
database.close()
}
private fun insertData(name: String) {
val values = ContentValues()
values.put("name", name)
database.insert("MyTable", null, values)
}
private fun getAllData(): Cursor {
return database.rawQuery("SELECT * FROM MyTable", null)
}
private fun updateData(oldName: String, newName: String) {
val values = ContentValues()
values.put("name", newName)
database.update("MyTable", values, "name = ?", arrayOf(oldName))
}
private fun deleteData(name: String) {
database.delete("MyTable", "name = ?", arrayOf(name))
}
@SuppressLint("Range")
private fun updateListView() {
val cursor = getAllData()
val data = ArrayList<String>()
if (cursor.moveToFirst()) {
do {
val name = cursor.getString(cursor.getColumnIndex("name"))
data.add(name)
} while (cursor.moveToNext())
}
cursor.close()
val adapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1, data)
listView.adapter = adapter
var lastClickTime = 0L
listView.setOnItemClickListener { _, _, position, _ ->
val now = System.currentTimeMillis()
if (now - lastClickTime < 500) { // Double-click interval is
500 milliseconds
val nameToUpdate = data[position]
val editText = EditText(this)
editText.setText(nameToUpdate)
val dialog = AlertDialog.Builder(this)
.setTitle("Update or Delete")
.setView(editText)
.setPositiveButton("Update") { _, _ ->
val newName = editText.text.toString()
updateData(nameToUpdate, newName)
updateListView()
}
.setNegativeButton("Delete") { _, _ ->
deleteData(nameToUpdate)
updateListView()
}
.setNeutralButton("Cancel", null)
.create()
dialog.show()
}
lastClickTime = now
}
}
}