Estoy aquí por la posibilidad de ayuda en la información de cómo realizar una condición que consiste en que un usuario introduzca un código y si el código es el mismo que contiene otra tabla en el cual anteriormente se registró, se registraran los datos aparte del código y si no es el mismo le mostrará un mensaje que este usuario es inexistente.
Este es el código de la base de datos:
package com.example.application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {
public AdminSQLiteOpenHelper(Context context,String name,SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase AdminRoom) {
AdminRoom.execSQL("create table alumnos(codebar text primary key unique, nombres text, apellidos text,semestre text, grupo text, turnos text, especialidad text)");
AdminRoom.execSQL("create table capturar(codebar text, salas text, hora_inicial text, hora_termino text, FOREIGN KEY(codebar) REFERENCES alumnos(codebar) )");
AdminRoom.execSQL("create table reportes(reporte TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase AdminRoom, int oldVersion, int newVersion) {
}
}
Formulario:
package com.example.application;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class Entrar extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
int txt;
String[] TimeInicio = new String[] {"7:00 AM", "8:00 AM","9:00 AM","10:00 AM","11:00 AM","12:00 AM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM","6:00 PM","7:00 PM","8:00 PM"};
String[] TimeTermino = new String[] {"8:00 AM","9:00 AM","10:00 AM","11:00 AM","12:00 AM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM","6:00 PM","7:00 PM","8:00 PM","9:00 PM"};
String[] Salas = new String[] {"Sala 1","Sala 2","Sala 3","Sala 4"};
EditText etCods;
Button btnEscanaers;
Spinner timei, timet, sala;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entrar);
timei = (Spinner) findViewById(R.id.spinnertmi);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, TimeInicio);
timei.setAdapter(adapter);
timet = (Spinner) findViewById(R.id.spinnertmt);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, TimeTermino);
timet.setAdapter(adapter2);
sala = (Spinner) findViewById(R.id.spinnersala);
ArrayAdapter<String> adapters = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Salas);
sala.setAdapter(adapters);
etCods=(EditText) findViewById(R.id.etCo);
btnEscanaers=(Button) findViewById(R.id.btsn);
btnEscanaers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
escaners();
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Buscar();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.entrar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_inicio) {
Intent enter = new Intent(Entrar.this, MainActivity.class);
startActivity(enter);
} else if (id == R.id.nav_enter) {
} else if (id == R.id.nav_register) {
Intent enter = new Intent(Entrar.this, Registrar.class);
startActivity(enter);
} else if (id == R.id.nav_help) {
Intent enter = new Intent(Entrar.this, Ayuda.class);
startActivity(enter);
} else if (id == R.id.nav_report) {
Intent enter = new Intent(Entrar.this, Reporte.class);
startActivity(enter);
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void Buscar(){
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,"administracion",null,1);
SQLiteDatabase AdminRoom = admin.getWritableDatabase();
AdminRoom.execSQL("PRAGMA foreign_keys = ON;");
String codebar = etCods.getText().toString();
String salas = sala.getSelectedItem().toString();
String hora_inicial = timei.getSelectedItem().toString();
String hora_termino = timet.getSelectedItem().toString();
if(!codebar.isEmpty()){
ContentValues capturars = new ContentValues();
capturars.put("codebar",codebar);
capturars.put("salas",salas);
capturars.put("hora_inicial",hora_inicial);
capturars.put("hora_termino",hora_termino);
try {
AdminRoom.insertOrThrow("capturar", null, capturars);
AdminRoom.close();
Toast.makeText(this, "Captura de Entrada Exitosa Exitosa. Ya puedes cerrar la aplicacion si lo deseas", Toast.LENGTH_LONG).show();
Intent enter = new Intent(Entrar.this, MainActivity.class);
startActivity(enter);
}catch (Exception e2){
MostrarAlertDialogE();
}
etCods.setText("");
}else {
MostrarAlertDialog();}
}
public void MostrarAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("ATENCION");
builder.setMessage("Rellena los campos en blanco para poder registrar tu entrada (Revisa que la informacion introducida sea correcta).");
builder.setPositiveButton("OK",null);
builder.create();
builder.show();
}
public void MostrarAlertDialogE() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("ATENCION");
builder.setMessage("El usuario con este codigo no exite,es necesario registrarlo para capturar la entrada.");
builder.setPositiveButton("OK",null);
builder.create();
builder.show();
}
public void escaners(){
IntentIntegrator intent = new IntentIntegrator(this);
intent.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
intent.setPrompt("ESCANEAR CODIGO");
intent.setCameraId(0);
intent.setBeepEnabled(false);
intent.setBarcodeImageEnabled(false);
intent.initiateScan();
intent.setOrientationLocked(false);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if( result != null){
if(result.getContents() == null){
Toast.makeText(this,"Escaneo Cancelado",Toast.LENGTH_LONG).show();
}else{
etCods.setText(result.getContents().toString());
}
}else{
super.onActivityResult(requestCode,resultCode,data);
}
}
}
Eh intentado realizar la condición pero no consigo tener el resultado esperado.Espero que puedan ayudarme y de antemano, Muchas Gracias.