EDITED
Good! The problem I'm having is that from my Data Base class I want to pass the value of 3 fields to another class called Choose Care. I managed to pass the value of two fields but I could not with the third. The field in question is the "id" field of the "reservations" table, and the two fields that could be passed from the same table are "date_ini" and "end_date". Then I leave my class BaseDeDatos:
public class BaseDeDatos extends SQLiteOpenHelper {
public BaseDeDatos(Context context) {
super(context, "petshotel.db", null, 8);
}
private static final String tabla_pets = "CREATE TABLE IF NOT EXISTS pets(id integer primary key autoincrement, nombre text, edad text, raza text, alergia text, reserva text)";
private static final String tabla = "pets";
private static final String tabla_cuidados = "CREATE TABLE IF NOT EXISTS cuidados(id integer primary key autoincrement, reserva text, dia text, turno text, baniar text, pasear text, cortar text, jugar text, comentario text)";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table reservas(id integer primary key autoincrement,usuario integer, nombre text, apellido text, correo text, telefono text, fecha_inicio text, fecha_fin text)");
db.execSQL("insert into reservas values(1, 1111, 'Rodrigo', 'Paz', '[email protected]', '1134720205', '2018-01-01', '2018-01-07'), (2, 2222, 'Juan', 'Perez', '[email protected]', '1165478954', '2018-02-01', '2018-02-05')");
db.execSQL(tabla_pets);
db.execSQL(tabla_cuidados);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS reservas");
db.execSQL("DROP TABLE IF EXISTS pets");
db.execSQL("DROP TABLE IF EXISTS cuidados");
db.execSQL("create table reservas(id integer primary key autoincrement,usuario integer, nombre text, apellido text, correo text, telefono text, fecha_inicio text, fecha_fin text)");
db.execSQL("insert into reservas values(1, 1111, 'Rodrigo', 'Paz', '[email protected]', '1134720205', '2018-01-01', '2018-01-07'), (2, 2222, 'Juan', 'Perez', '[email protected]', '1165478954', '2018-02-01', '2018-02-05')");
db.execSQL(tabla_pets);
db.execSQL(tabla_cuidados);
}
public void insertarPet(Integer id, String nombre, String edad, String raza, String alergia, String reserva) {
SQLiteDatabase bd = getWritableDatabase();
if(bd != null){
bd.execSQL("INSERT INTO pets VALUES(null, '"+nombre+"','"+edad+"','"+raza+"','"+alergia+"', '"+reserva+"')");
bd.close();
}
}
public void insertarCuidado(Integer id, String reserva, String dia, String turno, String baniar, String pasear, String cortar, String jugar, String comentario) {
SQLiteDatabase bd = getWritableDatabase();
if(bd != null){
bd.execSQL("INSERT INTO cuidados VALUES(null, '"+reserva+"','"+dia+"','"+turno+"','"+baniar+"','"+pasear+"','"+cortar+"','"+jugar+"','"+comentario+"')");
bd.close();
}
}
public List<Pets> mostrarPets(){
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM pets", null);
List<Pets> pets= new ArrayList<>();
if(cursor.moveToFirst()){
do {
pets.add(new Pets(cursor.getString(0),cursor.getString(1), cursor.getString(2), cursor.getString(3),
cursor.getString(4), cursor.getString(5)));
}while (cursor.moveToNext());
}
return pets;
}
public void eliminarTodo(){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM pets;");
Log.d("Eliminar pet","Datos borrados");
}
public Reserva getReserva(String id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM RESERVAS WHERE id = ?", new String[]{ id });
Reserva reserva = new Reserva();
if (cursor != null && cursor.moveToFirst()) {
reserva.setId(String.valueOf(cursor.getInt(0)));
reserva.setFecha_inicio(cursor.getString(6));
reserva.setFecha_fin(cursor.getString(7));
cursor.close();
db.close();
}
return reserva;
}
}
Below everything in the getReserva method is where I added up the fields dates, the id field to call it in the ChooseCare class, the problem is that in ChooseCare I do a Toast and instead of showing me the id value that I call, it shows me for some reason the value contained in the variable str_reserva (this was a provisional variable that I want to remove now to replace it with the value I want to bring in the "id" field), unlike the date fields that I do shows them correctly. I do not know what I'll be missing, this is the ChooseCare class where I'm calling:
public class ElegirCuidados extends AppCompatActivity {
private String reser;
String id, fecha_ini, fecha_fin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.elegir_cuidados);
Bundle extras = getIntent().getExtras();
reser = extras.getString("reserva");
Toast.makeText(getApplicationContext(), reser, Toast.LENGTH_SHORT).show();
if (extras != null) {
str_reserva = "2";
Reserva reserva;
BaseDeDatos bd = new BaseDeDatos(this);
reserva = bd.getReserva(str_reserva);
id = reserva.getId();
fecha_ini = reserva.getFecha_inicio();
fecha_fin = reserva.getFecha_fin();
Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();
TextView tvDesde = findViewById(R.id.tvDesde);
tvDesde.setText("Desde: " + fecha_ini);
TextView tvHasta = findViewById(R.id.tvHasta);
tvHasta.setText("Hasta: " + fecha_fin);