CRUD Di Android Studio
CRUD Di Android Studio
<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
editTextName = findViewById(R.id.editTextName)
listView = findViewById(R.id.listView)
database = dbHelper.writableDatabase
updateListView()
}
@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()
var lastClickTime = 0L
listView.setOnItemClickListener { _, _, position, _ ->
val now = System.currentTimeMillis()
if (now - lastClickTime < 500) { // Double-click interval is
500 milliseconds
val nameToUpdate = data[position]
dialog.show()
}
lastClickTime = now
}
}
}