Agregar Receta Terminado

This commit is contained in:
sebastiancc27 2024-04-25 22:01:59 -06:00
parent 8df8558f37
commit e83dc3fabe
6 changed files with 90 additions and 23 deletions

View File

@ -30,6 +30,7 @@ class AgregarActivity : AppCompatActivity() {
binding.edtxSexo.adapter = adapter binding.edtxSexo.adapter = adapter
binding.btnConfirmar.setOnClickListener { binding.btnConfirmar.setOnClickListener {
//db.crearTablaPacientes()
agregarPaciente() agregarPaciente()
val intent = Intent(this@AgregarActivity, EliminarActivity::class.java) val intent = Intent(this@AgregarActivity, EliminarActivity::class.java)
startActivity(intent) startActivity(intent)

View File

@ -49,6 +49,15 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA
val dropTablePacientes = "DROP TABLE IF EXISTS $TABLE_PACIENTES" val dropTablePacientes = "DROP TABLE IF EXISTS $TABLE_PACIENTES"
db?.execSQL(dropTablePacientes) 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){ fun agregarDoctores(doctor : Doctor, contrasena: String){
val db=writableDatabase val db=writableDatabase
val values = ContentValues().apply { val values = ContentValues().apply {
@ -112,4 +121,19 @@ class DoctorDataBaseHelper (context: Context) : SQLiteOpenHelper(context, DATABA
return isValid 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
}
} }

View File

@ -34,7 +34,7 @@ class MainActivity : AppCompatActivity() {
} }
binding.btnPrueba2.setOnClickListener { binding.btnPrueba2.setOnClickListener {
val intent = Intent(this@MainActivity, ConsultarPacientesActivity::class.java) val intent = Intent(this@MainActivity, RecetaActivity::class.java)
startActivity(intent) startActivity(intent)
} }
binding.tvRegistrarme.setOnClickListener{ binding.tvRegistrarme.setOnClickListener{

View File

@ -3,16 +3,22 @@ package com.terratenientes.medicalhealth
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import android.os.Bundle 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.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import com.terratenientes.medicalhealth.databinding.ActivityRecetaBinding import com.terratenientes.medicalhealth.databinding.ActivityRecetaBinding
class RecetaActivity : AppCompatActivity() { class RecetaActivity : AppCompatActivity(), OnItemSelectedListener {
private lateinit var binding : ActivityRecetaBinding private lateinit var binding : ActivityRecetaBinding
private lateinit var db : RecetaDataBaseHelper private lateinit var db : RecetaDataBaseHelper
private lateinit var dbPaciente : DoctorDataBaseHelper private lateinit var dbPaciente : DoctorDataBaseHelper
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding=ActivityRecetaBinding.inflate(layoutInflater) binding=ActivityRecetaBinding.inflate(layoutInflater)
@ -20,17 +26,18 @@ class RecetaActivity : AppCompatActivity() {
enableEdgeToEdge() enableEdgeToEdge()
db= RecetaDataBaseHelper(this@RecetaActivity) db= RecetaDataBaseHelper(this@RecetaActivity)
dbPaciente = DoctorDataBaseHelper(this@RecetaActivity) dbPaciente = DoctorDataBaseHelper(this@RecetaActivity)
setContentView(view) setContentView(view)
cargarSpinnerPacientes()
binding.spNombrePaciente.onItemSelectedListener=this
binding.btnAgregarReceta.setOnClickListener { binding.btnAgregarReceta.setOnClickListener {
agregarReceta()
} }
} }
@SuppressLint("Range") @SuppressLint("Range")
private fun obtenerNombrePacientes() : MutableList<String>{ private fun obtenerNombrePacientes() : ArrayList<String>{
val nombresPacientes = mutableListOf<String>() val nombresPacientes = arrayListOf<String>()
val dbPaciente=db.readableDatabase 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()){ while(cursor.moveToNext()){
val nombre = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_NOMBRE_PACIENTE)).toString() val nombre = cursor.getString(cursor.getColumnIndex(DoctorDataBaseHelper.COLUMN_NOMBRE_PACIENTE)).toString()
nombresPacientes.add(nombre) nombresPacientes.add(nombre)
@ -38,4 +45,32 @@ class RecetaActivity : AppCompatActivity() {
cursor.close() cursor.close()
return nombresPacientes 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<*>?) {
}
} }

View File

@ -1,5 +1,6 @@
package com.terratenientes.medicalhealth package com.terratenientes.medicalhealth
import android.content.ContentValues
import android.content.Context import android.content.Context
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
@ -30,4 +31,18 @@ class RecetaDataBaseHelper(context: Context) : SQLiteOpenHelper(context, DATABAE
val dropTableReceta="DROP TABLE IF EXISTS $TABLE_NAME" val dropTableReceta="DROP TABLE IF EXISTS $TABLE_NAME"
p0?.execSQL(dropTableReceta) p0?.execSQL(dropTableReceta)
} }
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()
}
} }

View File

@ -19,15 +19,13 @@
android:textColor="@color/primaryColor" android:textColor="@color/primaryColor"
android:textStyle="bold" android:textStyle="bold"
/> />
<EditText <Spinner
android:id="@+id/et_nombre_paciente" android:id="@+id/sp_nombre_paciente"
android:layout_marginTop="10dp" android:layout_width="300dp"
android:layout_width="300dp" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
android:hint="@string/receta_nombre_paciente"
android:layout_gravity="center_horizontal"
/>
/>
<EditText <EditText
android:id="@+id/et_edad_paciente" android:id="@+id/et_edad_paciente"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
@ -85,13 +83,7 @@
android:text="@string/btn_agregar_receta" android:text="@string/btn_agregar_receta"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
/> />
<Button
android:id="@+id/btn_ayuda_receta"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="@string/btn_ayuda_receta"
android:layout_gravity="center_horizontal"
/>
</LinearLayout> </LinearLayout>