From e83dc3fabe39f34370e45fa2cbf9a34b3855aa94 Mon Sep 17 00:00:00 2001 From: sebastiancc27 Date: Thu, 25 Apr 2024 22:01:59 -0600 Subject: [PATCH] Agregar Receta Terminado --- .../medicalhealth/AgregarActivity.kt | 1 + .../medicalhealth/DoctorDataBaseHelper.kt | 24 ++++++++++ .../medicalhealth/MainActivity.kt | 2 +- .../medicalhealth/RecetaActivity.kt | 47 ++++++++++++++++--- .../medicalhealth/RecetaDataBaseHelper.kt | 17 ++++++- app/src/main/res/layout/activity_receta.xml | 22 +++------ 6 files changed, 90 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt index 8648c75..0b1068d 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/AgregarActivity.kt @@ -30,6 +30,7 @@ class AgregarActivity : AppCompatActivity() { binding.edtxSexo.adapter = adapter binding.btnConfirmar.setOnClickListener { + //db.crearTablaPacientes() agregarPaciente() val intent = Intent(this@AgregarActivity, EliminarActivity::class.java) startActivity(intent) diff --git a/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt index 2a17563..6c67445 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/DoctorDataBaseHelper.kt @@ -49,6 +49,15 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA val dropTablePacientes = "DROP TABLE IF EXISTS $TABLE_PACIENTES" db?.execSQL(dropTablePacientes) } + fun crearTablaPacientes(){ + val db=writableDatabase + val crearTablaPacientes = + "CREATE TABLE $TABLE_PACIENTES($COLUMN_ID_PACIENTE INTEGER PRIMARY KEY AUTOINCREMENT, " + + "$COLUMN_NOMBRE_PACIENTE VARCHAR(30), $COLUMN_APELLIDO_PATERNO_PACIENTE VARCHAR(30), " + + "$COLUMN_APELLIDO_MATERNO_PACIENTE VARCHAR(30), $COLUMN_EDAD INTEGER, $COLUMN_SEXO VARCHAR(10), " + + "$COLUMN_DOMICILIO VARCHAR(100));" + db?.execSQL(crearTablaPacientes) + } fun agregarDoctores(doctor : Doctor, contrasena: String){ val db=writableDatabase val values = ContentValues().apply { @@ -112,4 +121,19 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA return isValid } + fun edadPaciente(nombrePaciente : String) : Int{ + val db=readableDatabase + var edadPaciente =0 + val queryEdad= "SELECT ${COLUMN_EDAD} FROM ${TABLE_PACIENTES} WHERE ${COLUMN_NOMBRE_PACIENTE}='${nombrePaciente}'" + val cursor = db.rawQuery(queryEdad,null) + if(cursor.moveToFirst()){ + val columnEdad = cursor.getColumnIndex(COLUMN_EDAD) + edadPaciente=cursor.getInt(columnEdad) + } + cursor.close() + db.close() + return edadPaciente + } + + } diff --git a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt index 60ce44a..6cb7671 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/MainActivity.kt @@ -34,7 +34,7 @@ class MainActivity : AppCompatActivity() { } binding.btnPrueba2.setOnClickListener { - val intent = Intent(this@MainActivity, ConsultarPacientesActivity::class.java) + val intent = Intent(this@MainActivity, RecetaActivity::class.java) startActivity(intent) } binding.tvRegistrarme.setOnClickListener{ diff --git a/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt index 590c2ae..7787101 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt @@ -3,16 +3,22 @@ package com.terratenientes.medicalhealth import android.annotation.SuppressLint import android.database.sqlite.SQLiteOpenHelper import android.os.Bundle +import android.view.View +import android.widget.AdapterView +import android.widget.AdapterView.OnItemSelectedListener +import android.widget.ArrayAdapter +import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import com.terratenientes.medicalhealth.databinding.ActivityRecetaBinding -class RecetaActivity : AppCompatActivity() { +class RecetaActivity : AppCompatActivity(), OnItemSelectedListener { private lateinit var binding : ActivityRecetaBinding private lateinit var db : RecetaDataBaseHelper private lateinit var dbPaciente : DoctorDataBaseHelper + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding=ActivityRecetaBinding.inflate(layoutInflater) @@ -20,17 +26,18 @@ class RecetaActivity : AppCompatActivity() { enableEdgeToEdge() db= RecetaDataBaseHelper(this@RecetaActivity) dbPaciente = DoctorDataBaseHelper(this@RecetaActivity) - setContentView(view) + cargarSpinnerPacientes() + binding.spNombrePaciente.onItemSelectedListener=this binding.btnAgregarReceta.setOnClickListener { - + agregarReceta() } } @SuppressLint("Range") - private fun obtenerNombrePacientes() : MutableList{ - val nombresPacientes = mutableListOf() + private fun obtenerNombrePacientes() : ArrayList{ + val nombresPacientes = arrayListOf() val dbPaciente=db.readableDatabase - val cursor = dbPaciente.rawQuery("SELECT NombrePaciente FROM Pacientes", null) + val cursor = dbPaciente.rawQuery("SELECT ${DoctorDataBaseHelper.COLUMN_NOMBRE_PACIENTE} FROM ${DoctorDataBaseHelper.TABLE_PACIENTES}", null) while(cursor.moveToNext()){ val nombre = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_NOMBRE_PACIENTE)).toString() nombresPacientes.add(nombre) @@ -38,4 +45,32 @@ class RecetaActivity : AppCompatActivity() { cursor.close() return nombresPacientes } + private fun cargarSpinnerPacientes(){ + val items=obtenerNombrePacientes() + val spinner_adapter=ArrayAdapter(this@RecetaActivity,android.R.layout.simple_spinner_item, items) + spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) + binding.spNombrePaciente.adapter=spinner_adapter + } + private fun agregarReceta(){ + val nombrePaciente = binding.spNombrePaciente.selectedItem.toString() + val edadPaciente = (binding.etEdadPaciente.text.toString()).toInt() + val pesoPaciente = (binding.etPesoPaciente.text.toString()).toDouble() + val medicamento= binding.etRecetaMedicamento.text.toString() + val fecha=binding.etFecha.text.toString() + val dosis = binding.etDosisMedicamento.text.toString() + val duracion= binding.etDuracion.text.toString() + val receta = Receta(nombrePaciente,edadPaciente,pesoPaciente,medicamento,fecha,dosis,duracion) + db.agregarReceta(receta) + Toast.makeText(this@RecetaActivity , "RECETA AGREGADA CORRECTAMENTE", Toast.LENGTH_LONG).show() + } + + override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) { + var nombre=binding.spNombrePaciente.selectedItem.toString() + val edad=dbPaciente.edadPaciente(nombre).toString() + binding.etEdadPaciente.setText(edad) + } + + override fun onNothingSelected(p0: AdapterView<*>?) { + + } } \ No newline at end of file diff --git a/app/src/main/java/com/terratenientes/medicalhealth/RecetaDataBaseHelper.kt b/app/src/main/java/com/terratenientes/medicalhealth/RecetaDataBaseHelper.kt index 716a64a..b79a037 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/RecetaDataBaseHelper.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/RecetaDataBaseHelper.kt @@ -1,5 +1,6 @@ package com.terratenientes.medicalhealth +import android.content.ContentValues import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper @@ -30,4 +31,18 @@ class RecetaDataBaseHelper(context: Context) : SQLiteOpenHelper(context, DATABAE val dropTableReceta="DROP TABLE IF EXISTS $TABLE_NAME" p0?.execSQL(dropTableReceta) } -} \ No newline at end of file + fun agregarReceta(receta : Receta){ + val db = writableDatabase + val values = ContentValues().apply { + put(COLUMN_NOMBRE_PACIENTE, receta.NombrePaciente) + put(COLUMN_EDAD_PACIENTE, receta.edadPaciente) + put(COLUMN_PESO_PACIENTE, receta.pesoPaciente) + put(COLUMN_MEDICAMENTO, receta.nombreMedicamento) + put(COLUMN_FECHA,receta.fecha) + put(COLUMN_DOSIS_PACIENTE, receta.dosis) + put(DURACION_MEDICAMENTO, receta.duracion) + } + db.insert(TABLE_NAME, null, values) + db.close() + } +} diff --git a/app/src/main/res/layout/activity_receta.xml b/app/src/main/res/layout/activity_receta.xml index 4215a46..c4d0cc1 100644 --- a/app/src/main/res/layout/activity_receta.xml +++ b/app/src/main/res/layout/activity_receta.xml @@ -19,15 +19,13 @@ android:textColor="@color/primaryColor" android:textStyle="bold" /> - + -