From 0280d87198652eae1d7bd217a3050999d0032299 Mon Sep 17 00:00:00 2001 From: mosterTeco Date: Sun, 21 Apr 2024 11:08:31 -0600 Subject: [PATCH] Funcionalidad Pantalla Eliminar --- .idea/.name | 1 + .../medicalhealth/AgregarActivity.kt | 3 + .../medicalhealth/DoctorDataBaseHelper.kt | 24 +++----- .../medicalhealth/EliminarActivity.kt | 60 ++++++++++++++++++- .../terratenientes/medicalhealth/Paciente.kt | 11 ++++ .../medicalhealth/PacienteAdapter.kt | 30 ++++++++++ .../list_item_selected_background.xml | 8 +++ .../res/layout/activity_eliminar_paciente.xml | 3 +- 8 files changed, 122 insertions(+), 18 deletions(-) create mode 100644 .idea/.name create mode 100644 app/src/main/java/com/terratenientes/medicalhealth/Paciente.kt create mode 100644 app/src/main/java/com/terratenientes/medicalhealth/PacienteAdapter.kt create mode 100644 app/src/main/res/drawable/list_item_selected_background.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..a8e6934 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +MedicalHealth \ No newline at end of file diff --git a/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt index 52bccc0..3a158bc 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt @@ -1,5 +1,6 @@ package com.terratenientes.medicalhealth +import android.content.Intent import android.os.Bundle import android.widget.Toast import androidx.activity.enableEdgeToEdge @@ -23,6 +24,8 @@ class AgregarActivity : AppCompatActivity() { binding.btnConfirmar.setOnClickListener { agregarPaciente() + val intent = Intent(this@AgregarActivity, EliminarActivity::class.java) + startActivity(intent) } } private fun agregarPaciente() { diff --git a/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt index 9181da4..0ecedd0 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt @@ -19,14 +19,14 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA private const val COLUMN_TELEFONO="Telefono" private const val COLUMN_CONSULTORIO="Consultorio" private const val COLUMN_CONTRASENA="Contrasena" - private const val TABLE_PACIENTES = "Pacientes" - private const val COLUMN_ID_PACIENTE = "IdPaciente" - private const val COLUMN_NOMBRE_PACIENTE = "NombrePaciente" - private const val COLUMN_APELLIDO_PATERNO_PACIENTE = "ApellidoPaternoPaciente" - private const val COLUMN_APELLIDO_MATERNO_PACIENTE = "ApellidoMaternoPaciente" - private const val COLUMN_EDAD = "Edad" - private const val COLUMN_SEXO = "Sexo" - private const val COLUMN_DOMICILIO = "Domicilio" + const val TABLE_PACIENTES = "Pacientes" + const val COLUMN_ID_PACIENTE = "IdPaciente" + const val COLUMN_NOMBRE_PACIENTE = "NombrePaciente" + const val COLUMN_APELLIDO_PATERNO_PACIENTE = "ApellidoPaternoPaciente" + const val COLUMN_APELLIDO_MATERNO_PACIENTE = "ApellidoMaternoPaciente" + const val COLUMN_EDAD = "Edad" + const val COLUMN_SEXO = "Sexo" + const val COLUMN_DOMICILIO = "Domicilio" } @SuppressLint("SuspiciousIndentation") override fun onCreate(db: SQLiteDatabase?) { @@ -95,12 +95,4 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA return isValid } - fun eliminarPaciente(paciente: Paciente) { - val db = writableDatabase - val selection = "$COLUMN_ID_PACIENTE = ?" - val selectionArgs = arrayOf(paciente.id.toString()) - db.delete(TABLE_PACIENTES, selection, selectionArgs) - db.close() - } - } diff --git a/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt index 8205c88..ab37086 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/EliminarActivity.kt @@ -1,6 +1,11 @@ package com.terratenientes.medicalhealth +import android.R +import android.annotation.SuppressLint import android.os.Bundle +import android.util.Log +import android.widget.ArrayAdapter +import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat @@ -12,11 +17,64 @@ class EliminarActivity : AppCompatActivity() { private lateinit var binding: ActivityEliminarPacienteBinding private lateinit var db: DoctorDataBaseHelper + private lateinit var pacientesList: MutableList + private var pacienteSeleccionado: Paciente? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityEliminarPacienteBinding.inflate(layoutInflater) val view = binding.root setContentView(view) db = DoctorDataBaseHelper(this@EliminarActivity) + + cargarListaPacientes() + + binding.listaPacientes.setOnItemClickListener { _, _, position, _ -> + pacienteSeleccionado = pacientesList[position] + } + + binding.btnEliminarPaciente.setOnClickListener { + pacienteSeleccionado?.let { + eliminarPaciente(it) + pacienteSeleccionado = null + cargarListaPacientes() + } ?: run { + Toast.makeText(this, "Por favor, selecciona un paciente para eliminar.", Toast.LENGTH_SHORT).show() + } + } } -} \ No newline at end of file + + private fun cargarListaPacientes() { + pacientesList = obtenerListaPacientes() + + // Crear un adaptador personalizado y configurarlo en el ListView + val adapter = PacienteAdapter(this, pacientesList) + binding.listaPacientes.adapter = adapter + } + + @SuppressLint("Range") + private fun obtenerListaPacientes(): MutableList { + val pacientes = mutableListOf() + val db = db.readableDatabase + val cursor = db.rawQuery("SELECT * FROM ${DoctorDataBaseHelper.TABLE_PACIENTES}", null) + while (cursor.moveToNext()) { + val id = cursor.getInt(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_ID_PACIENTE)) + val nombre = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_NOMBRE_PACIENTE)) + val apellidoPaterno = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_APELLIDO_PATERNO_PACIENTE)) + val apellidoMaterno = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_APELLIDO_MATERNO_PACIENTE)) + val edad = cursor.getInt(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_EDAD)) + val sexo = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_SEXO)) + val domicilio = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_DOMICILIO)) + val paciente = Paciente(id, nombre, apellidoPaterno, apellidoMaterno, edad, sexo, domicilio) + pacientes.add(paciente) + } + cursor.close() + return pacientes + } + + private fun eliminarPaciente(paciente: Paciente) { + val db = db.writableDatabase + db.delete(DoctorDataBaseHelper.TABLE_PACIENTES, "${DoctorDataBaseHelper.COLUMN_ID_PACIENTE} = ?", arrayOf(paciente.id.toString())) + db.close() + } +} diff --git a/app/src/main/java/com/terratenientes/medicalhealth/Paciente.kt b/app/src/main/java/com/terratenientes/medicalhealth/Paciente.kt new file mode 100644 index 0000000..f3eda22 --- /dev/null +++ b/app/src/main/java/com/terratenientes/medicalhealth/Paciente.kt @@ -0,0 +1,11 @@ +package com.terratenientes.medicalhealth + +data class Paciente( + var id: Int, // Este campo representa el ID autoincrementable en la base de datos + var nombre: String, + var apellidoPaterno: String, + var apellidoMaterno: String, + var edad: Int, + var sexo: String, + var domicilio: String +) \ No newline at end of file diff --git a/app/src/main/java/com/terratenientes/medicalhealth/PacienteAdapter.kt b/app/src/main/java/com/terratenientes/medicalhealth/PacienteAdapter.kt new file mode 100644 index 0000000..3a7fa8d --- /dev/null +++ b/app/src/main/java/com/terratenientes/medicalhealth/PacienteAdapter.kt @@ -0,0 +1,30 @@ +package com.terratenientes.medicalhealth + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import android.widget.TextView + +class PacienteAdapter(context: Context, private val pacientes: List) : ArrayAdapter(context, 0, pacientes) { + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + var itemView = convertView + if (itemView == null) { + itemView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_2, parent, false) + } + + val paciente = pacientes[position] + + // Buscar los TextView dentro del layout simple_list_item_2 + val text1 = itemView?.findViewById(android.R.id.text1) + val text2 = itemView?.findViewById(android.R.id.text2) + + // Establecer el nombre y el ID del paciente en los TextView + text1?.text = "${paciente.nombre} ${paciente.apellidoPaterno} ${paciente.apellidoMaterno}" + text2?.text = "ID: ${paciente.id}" + + return itemView!! + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/list_item_selected_background.xml b/app/src/main/res/drawable/list_item_selected_background.xml new file mode 100644 index 0000000..42be763 --- /dev/null +++ b/app/src/main/res/drawable/list_item_selected_background.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_eliminar_paciente.xml b/app/src/main/res/layout/activity_eliminar_paciente.xml index 91ca2e8..af42305 100644 --- a/app/src/main/res/layout/activity_eliminar_paciente.xml +++ b/app/src/main/res/layout/activity_eliminar_paciente.xml @@ -24,7 +24,8 @@ android:layout_height="195dp" android:layout_marginStart="20dp" android:layout_marginTop="10dp" - android:layout_marginEnd="5dp" /> + android:layout_marginEnd="5dp" + android:listSelector="@drawable/list_item_selected_background"/>