|
@@ -0,0 +1,195 @@
|
|
|
+package korobkinapp.example.sql
|
|
|
+
|
|
|
+import android.os.Bundle
|
|
|
+import android.view.View
|
|
|
+import android.widget.EditText
|
|
|
+import android.widget.Toast
|
|
|
+import kotlinx.android.synthetic.main.activity_main.*
|
|
|
+import android.content.DialogInterface
|
|
|
+import androidx.appcompat.app.AlertDialog
|
|
|
+import androidx.appcompat.app.AppCompatActivity
|
|
|
+
|
|
|
+
|
|
|
+class MainActivity : AppCompatActivity() {
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+ setContentView(R.layout.activity_main)
|
|
|
+ //creating the instance of DatabaseHandler class
|
|
|
+ val databaseHandler: DatabaseHandler= DatabaseHandler(this)
|
|
|
+ //calling the viewEmployee method of DatabaseHandler class to read the records
|
|
|
+ val emp: List<EmpModelClass> = databaseHandler.viewEmployee()
|
|
|
+ val empArrayId = Array<String>(emp.size){"0"}
|
|
|
+ val empArrayName = Array<String>(emp.size){"null"}
|
|
|
+ val empArrayDescription = Array<String>(emp.size){"null"}
|
|
|
+ var index = 0
|
|
|
+ for(e in emp){
|
|
|
+ empArrayId[index] = e.userId.toString()
|
|
|
+ empArrayName[index] = e.userName
|
|
|
+ empArrayDescription[index] = e.userDescription
|
|
|
+ index++
|
|
|
+ }
|
|
|
+ //creating custom ArrayAdapter
|
|
|
+ val myListAdapter = MyListAdapter(this,empArrayId,empArrayName,empArrayDescription)
|
|
|
+ listView.adapter = myListAdapter
|
|
|
+ }
|
|
|
+ //method for saving records in database
|
|
|
+ fun saveRecord(view: View){
|
|
|
+ val id = u_id.text.toString()
|
|
|
+ val name = u_name.text.toString()
|
|
|
+ val description = u_description.text.toString()
|
|
|
+ val databaseHandler: DatabaseHandler= DatabaseHandler(this)
|
|
|
+ if(id.trim()!="" && name.trim()!="" && description.trim()!=""){
|
|
|
+ val status = databaseHandler.addEmployee(EmpModelClass(Integer.parseInt(id),name, description))
|
|
|
+ if(status > -1){
|
|
|
+ Toast.makeText(applicationContext,"record save",Toast.LENGTH_LONG).show()
|
|
|
+ u_id.text.clear()
|
|
|
+ u_name.text.clear()
|
|
|
+ u_description.text.clear()
|
|
|
+
|
|
|
+ //creating the instance of DatabaseHandler class
|
|
|
+ val databaseHandler: DatabaseHandler= DatabaseHandler(this)
|
|
|
+ //calling the viewEmployee method of DatabaseHandler class to read the records
|
|
|
+ val emp: List<EmpModelClass> = databaseHandler.viewEmployee()
|
|
|
+ val empArrayId = Array<String>(emp.size){"0"}
|
|
|
+ val empArrayName = Array<String>(emp.size){"null"}
|
|
|
+ val empArrayDescription = Array<String>(emp.size){"null"}
|
|
|
+ var index = 0
|
|
|
+ for(e in emp){
|
|
|
+ empArrayId[index] = e.userId.toString()
|
|
|
+ empArrayName[index] = e.userName
|
|
|
+ empArrayDescription[index] = e.userDescription
|
|
|
+ index++
|
|
|
+ }
|
|
|
+ //creating custom ArrayAdapter
|
|
|
+ val myListAdapter = MyListAdapter(this,empArrayId,empArrayName,empArrayDescription)
|
|
|
+ listView.adapter = myListAdapter
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ Toast.makeText(applicationContext,"id or name or description cannot be blank",Toast.LENGTH_LONG).show()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ //method for read records from database in ListView
|
|
|
+ fun viewRecord(view: View){
|
|
|
+ val dialogBuilder = AlertDialog.Builder(this)
|
|
|
+ val inflater = this.layoutInflater
|
|
|
+ val dialogView = inflater.inflate(R.layout.find_dialog, null)
|
|
|
+ dialogBuilder.setView(dialogView)
|
|
|
+ val fndId = dialogView.findViewById(R.id.findId) as EditText
|
|
|
+ dialogBuilder.setTitle("Find")
|
|
|
+ dialogBuilder.setMessage("Enter data below")
|
|
|
+ dialogBuilder.setPositiveButton("Find", DialogInterface.OnClickListener { _, _ ->
|
|
|
+ val findId = fndId.text.toString() })
|
|
|
+ dialogBuilder.setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, which ->
|
|
|
+ })
|
|
|
+ val b = dialogBuilder.create()
|
|
|
+ b.show()
|
|
|
+ }
|
|
|
+ //method for updating records based on user id
|
|
|
+ fun updateRecord(view: View){
|
|
|
+ val dialogBuilder = AlertDialog.Builder(this)
|
|
|
+ val inflater = this.layoutInflater
|
|
|
+ val dialogView = inflater.inflate(R.layout.update_dialog, null)
|
|
|
+ dialogBuilder.setView(dialogView)
|
|
|
+
|
|
|
+ val edtId = dialogView.findViewById(R.id.updateId) as EditText
|
|
|
+ val edtName = dialogView.findViewById(R.id.updateName) as EditText
|
|
|
+ val edtDescription = dialogView.findViewById(R.id.updateDescription) as EditText
|
|
|
+
|
|
|
+ dialogBuilder.setTitle("Update Record")
|
|
|
+ dialogBuilder.setMessage("Enter data below")
|
|
|
+ dialogBuilder.setPositiveButton("Update", DialogInterface.OnClickListener { _, _ ->
|
|
|
+
|
|
|
+ val updateId = edtId.text.toString()
|
|
|
+ val updateName = edtName.text.toString()
|
|
|
+ val updateDescription = edtDescription.text.toString()
|
|
|
+ //creating the instance of DatabaseHandler class
|
|
|
+ val databaseHandler: DatabaseHandler= DatabaseHandler(this)
|
|
|
+ if(updateId.trim()!="" && updateName.trim()!="" && updateDescription.trim()!=""){
|
|
|
+ //calling the updateEmployee method of DatabaseHandler class to update record
|
|
|
+ val status = databaseHandler.updateEmployee(EmpModelClass(Integer.parseInt(updateId),updateName, updateDescription))
|
|
|
+ if(status > -1){
|
|
|
+ Toast.makeText(applicationContext,"record update",Toast.LENGTH_LONG).show()
|
|
|
+
|
|
|
+ //creating the instance of DatabaseHandler class
|
|
|
+ val databaseHandler: DatabaseHandler= DatabaseHandler(this)
|
|
|
+ //calling the viewEmployee method of DatabaseHandler class to read the records
|
|
|
+ val emp: List<EmpModelClass> = databaseHandler.viewEmployee()
|
|
|
+ val empArrayId = Array<String>(emp.size){"0"}
|
|
|
+ val empArrayName = Array<String>(emp.size){"null"}
|
|
|
+ val empArrayDescription = Array<String>(emp.size){"null"}
|
|
|
+ var index = 0
|
|
|
+ for(e in emp){
|
|
|
+ empArrayId[index] = e.userId.toString()
|
|
|
+ empArrayName[index] = e.userName
|
|
|
+ empArrayDescription[index] = e.userDescription
|
|
|
+ index++
|
|
|
+ }
|
|
|
+ //creating custom ArrayAdapter
|
|
|
+ val myListAdapter = MyListAdapter(this,empArrayId,empArrayName,empArrayDescription)
|
|
|
+ listView.adapter = myListAdapter
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ Toast.makeText(applicationContext,"id or name or description cannot be blank",Toast.LENGTH_LONG).show()
|
|
|
+ }
|
|
|
+
|
|
|
+ })
|
|
|
+ dialogBuilder.setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, which ->
|
|
|
+ //pass
|
|
|
+ })
|
|
|
+ val b = dialogBuilder.create()
|
|
|
+ b.show()
|
|
|
+ }
|
|
|
+ //method for deleting records based on id
|
|
|
+ fun deleteRecord(view: View){
|
|
|
+ //creating AlertDialog for taking user id
|
|
|
+ val dialogBuilder = AlertDialog.Builder(this)
|
|
|
+ val inflater = this.layoutInflater
|
|
|
+ val dialogView = inflater.inflate(R.layout.delete_dialog, null)
|
|
|
+ dialogBuilder.setView(dialogView)
|
|
|
+
|
|
|
+ val dltId = dialogView.findViewById(R.id.deleteId) as EditText
|
|
|
+ dialogBuilder.setTitle("Delete Record")
|
|
|
+ dialogBuilder.setMessage("Enter id below")
|
|
|
+ dialogBuilder.setPositiveButton("Delete", DialogInterface.OnClickListener { _, _ ->
|
|
|
+
|
|
|
+ val deleteId = dltId.text.toString()
|
|
|
+ //creating the instance of DatabaseHandler class
|
|
|
+ val databaseHandler: DatabaseHandler= DatabaseHandler(this)
|
|
|
+ if(deleteId.trim()!=""){
|
|
|
+ //calling the deleteEmployee method of DatabaseHandler class to delete record
|
|
|
+ val status = databaseHandler.deleteEmployee(EmpModelClass(Integer.parseInt(deleteId),"",""))
|
|
|
+ if(status > -1){
|
|
|
+ Toast.makeText(applicationContext,"record deleted",Toast.LENGTH_LONG).show()
|
|
|
+
|
|
|
+ //creating the instance of DatabaseHandler class
|
|
|
+ val databaseHandler: DatabaseHandler= DatabaseHandler(this)
|
|
|
+ //calling the viewEmployee method of DatabaseHandler class to read the records
|
|
|
+ val emp: List<EmpModelClass> = databaseHandler.viewEmployee()
|
|
|
+ val empArrayId = Array<String>(emp.size){"0"}
|
|
|
+ val empArrayName = Array<String>(emp.size){"null"}
|
|
|
+ val empArrayDescription = Array<String>(emp.size){"null"}
|
|
|
+ var index = 0
|
|
|
+ for(e in emp){
|
|
|
+ empArrayId[index] = e.userId.toString()
|
|
|
+ empArrayName[index] = e.userName
|
|
|
+ empArrayDescription[index] = e.userDescription
|
|
|
+ index++
|
|
|
+ }
|
|
|
+ //creating custom ArrayAdapter
|
|
|
+ val myListAdapter = MyListAdapter(this,empArrayId,empArrayName,empArrayDescription)
|
|
|
+ listView.adapter = myListAdapter
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ Toast.makeText(applicationContext,"id or name or description cannot be blank",Toast.LENGTH_LONG).show()
|
|
|
+ }
|
|
|
+
|
|
|
+ })
|
|
|
+ dialogBuilder.setNegativeButton("Cancel", DialogInterface.OnClickListener { _, _ ->
|
|
|
+ //pass
|
|
|
+ })
|
|
|
+ val b = dialogBuilder.create()
|
|
|
+ b.show()
|
|
|
+ }
|
|
|
+}
|