I have a program of 2 activities, the initial activity is a log that connects to a bd by remote control, establishes the connection if the user and password are correct.
Now in activity two I need you to show me in a TextView
the name of the user that started the session. How do I do that?
I leave part of the code here to give you an idea:
Activity 1 all right works included exceptions and connects to the bd
public class MainActivity extends AppCompatActivity {
Connection conn;
Button btnlogin;
EditText euser,epass;
TextView tuser,tpass;
String[]usuarios;
String CUsuario;
String Pwd;
Boolean prueba=false,prueba2=false;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnlogin = (Button) findViewById(R.id.BtnAceptar);
euser=(EditText) findViewById(R.id.TxtUsuario);
epass = (EditText) findViewById(R.id.TxtPassword);
tuser = (TextView) findViewById(R.id.TextView01);
tpass = (TextView) findViewById(R.id.TextView02);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pwd= epass.getText().toString();
CUsuario=euser.getText().toString();
Login tarea6=new Login(euser.getText().toString(),epass.getText().toString());
tarea6.execute();
}
});
}
private class Login extends AsyncTask<String, Integer, Boolean> {
public Login(String usuario, String contraseña) {
super();
}
@Override
protected Boolean doInBackground(String...usuario) {
conn = DBConnection.getInstance().getConnection();
try {
prueba = false;
prueba2=false;
String stsql = "SELECT CUsuario,Pwd FROM dbo.xUser WHERE CUsuario='"+CUsuario+"' or Pwd='"+Pwd+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(stsql);
while (rs.next()) {
if(CUsuario.equals(rs.getString(1)) && Pwd.equals(rs.getString(2))) {
prueba = true;
CUsuario = rs.getString(1);
Pwd = rs.getString(2);
}
else if(CUsuario.equals(rs.getString(1))) {
prueba2=true;
}
}
}catch (SQLException e1) {
e1.printStackTrace();
}
publishProgress(100);
return true;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (CUsuario.equals("") || Pwd.equals("")) {
Toast.makeText(getApplicationContext(),"inserta un usuario y una contraseña", Toast.LENGTH_SHORT).show();
euser.setText("");
epass.setText("");
}
else if (prueba==true) {
Toast.makeText(getApplicationContext(), "acceso permitido", Toast.LENGTH_SHORT).show();
euser.setText("");
epass.setText("");
Intent siguiente = new Intent(MainActivity.this, Main2Activity.class);
siguiente.putExtra(CUsuario, euser.getText().toString());
startActivity(siguiente);
prueba=false;
}
else if ((prueba == false)&&(prueba2==false)) {
Toast.makeText(getApplicationContext(), "contraseña y usuario incorrectos", Toast.LENGTH_SHORT).show();
euser.setText("");
epass.setText("");
}else{
Toast.makeText(getApplicationContext(), "usuario correcto,contraseña incorrecta", Toast.LENGTH_SHORT).show();
epass.setText("");
}
}
@Override
protected void onProgressUpdate(Integer... values) {//este es para interactuar con la interfaz grafica mientras esta en ejecucion la tarea asyncrona
int progreso = values[0].intValue();
}
}
Activity 2
public class Main2Activity extends Activity {
Button btn1,btn2,btn3;
public TextView t1;
Connection conn3,conn;
String[]llamadas;
int IdLlamada;
Date Fecha;
String Mensaje;
String De;
String IdUsuarioDestino;
String NombreUsuario;
String CUsuario;
boolean Estado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
t1=(TextView)findViewById(R.id.t1);
btn1= (Button) findViewById(R.id.Btnllamadasatendidas);
btn2= (Button) findViewById(R.id.Btnllamadasperdidas);
btn3= (Button) findViewById(R.id.Btncerrar);
NombreUsuario=getIntent().getExtras().getString(CUsuario);
Intent intent = getIntent();
String user= intent.getStringExtra( NombreUsuario);
t1.setText(user);
I think the bug is in the username in the CUsuario
it does not let me get the EUser
of the other activity and pass CUsuario
and it's empty, any solution?