यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android SQLite डेटाबेस में डेटा इन्सर्ट करने के बाद ListView को कैसे अपडेट किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में UI बनाएं
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />
<EditText
android:id="@+id/salary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Salary"
android:inputType="numberDecimal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
चरण 3: MainActivity.kt में लॉजिक जोड़ें
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var save: Button
private lateinit var refresh: Button
private lateinit var name: EditText
private lateinit var salary: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val helper = DatabaseHelper(this)
val arrayList: ArrayList<String> = helper.getAllContacts() as ArrayList<String>
name = findViewById(R.id.name)
salary = findViewById(R.id.salary)
save = findViewById(R.id.save)
refresh = findViewById(R.id.refresh)
val listView: ListView = findViewById(R.id.listView)
val arrayAdapter: ArrayAdapter<*> = ArrayAdapter(
this@MainActivity,
android.R.layout.simple_list_item_1,
arrayList as List<Any?>
)
listView.adapter = arrayAdapter
save.setOnClickListener {
arrayList.clear()
arrayList.addAll(helper.getAllContacts())
arrayAdapter.notifyDataSetChanged()
listView.invalidateViews()
listView.refreshDrawableState()
}
refresh.setOnClickListener {
if (name.text.toString().isNotEmpty() && salary.text.toString().isNotEmpty()) {
if (helper.addData(name.text.toString(), salary.text.toString())) {
Toast.makeText(this, "Inserted", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "NOT Inserted", Toast.LENGTH_LONG).show()
}
} else {
name.error = "Enter NAME"
salary.error = "Enter Salary"
}
}
}
}
चरण 4: DatabaseHelper.kt बनाएं
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteException
import android.database.sqlite.SQLiteOpenHelper
import java.io.IOException
class DatabaseHelper(context: Context) : SQLiteOpenHelper(context, dataBaseName, null, dataBaseVersion) {
private val contactsTableName = "SalaryDetails"
companion object {
const val dataBaseName = "salaryDatabase3"
const val dataBaseVersion = 1
}
override fun onCreate(db: SQLiteDatabase?) {
try {
db?.execSQL("CREATE TABLE $contactsTableName(id INTEGER PRIMARY KEY, name TEXT, salary TEXT)")
} catch (e: SQLiteException) {
try {
throw IOException(e)
} catch (e1: IOException) {
e1.printStackTrace()
}
}
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db?.execSQL("DROP TABLE IF EXISTS $contactsTableName")
onCreate(db)
}
fun addData(name: String?, salary: String?): Boolean {
val db = this.writableDatabase
val contentValues = ContentValues().apply {
put("name", name)
put("salary", salary)
}
db.insert(contactsTableName, null, contentValues)
return true
}
fun getAllContacts(): Collection<String> {
val db: SQLiteDatabase = this.readableDatabase
val arrayList = ArrayList<String>()
val res: Cursor = db.rawQuery("SELECT * FROM $contactsTableName", null)
res.moveToFirst()
while (!res.isAfterLast) {
arrayList.add(res.getString(res.getColumnIndex("name")))
res.moveToNext()
}
res.close()
return arrayList
}
}
चरण 5: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।