Help with android studio various activitys, obtaining data

2

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?

    
asked by TAMARUSS 10.11.2016 в 11:43
source

2 answers

5

The problem is that the name of the key in the preference is being modified, when you send a data it defines a key name and when receiving you get the bundle the same value.

Do this Intent :

  Intent siguiente = new Intent(MainActivity.this, Main2Activity.class);
  //siguiente.putExtra(CUsuario, euser.getText().toString());
  siguiente.putExtra("usuario", euser.getText().toString());
  startActivity(siguiente);

when receiving the value of "user" in the other Activity , simply define the name of the key of the value you want to obtain:

NombreUsuario = getIntent().getExtras().getString("usuario");

or simply:

NombreUsuario = getIntent().getStringExtra("usuario");

In this way the variable NombreUsuario in your class Main2Activity , will be obtained correctly.

How to send data between Activities.

To send the data is generally done by a Bundle in which values can be added and that bundle is sent through a Intent . You can specify the sending of any type of element or element array by specifying the name:

    intent.putExtra("usuario", "StackOverflow!");
    intent.putExtra("id", 123);
    intent.putExtra("myByte", 0xa);
    intent.putExtra("latitud", 0.12324234);
    startActivity(intent);      

The values are obtained in the Activity that the Bundle receives via getExtras () or the specific method for getting data type received . Return null if you can not find value.

String valor = getIntent().getExtras().getString("usuario");

or simply:

String valor = getIntent().getStringExtra("usuario");
    
answered by 10.11.2016 в 15:56
1

I think you are not taking the value because in the Main2Activity the value of CU is not the same as in the MainActivity, so you will not find that parameter when trying to recover it.

In the MainActivity you are sending the information to the Main2Activity in the following way:

String CUsuario;
CUsuario = euser.getText().toString();
.....
Intent siguiente = new Intent(MainActivity.this, Main2Activity.class);
siguiente.putExtra(CUsuario, euser.getText().toString());

And in the Main2Activity you are collecting it in this way:

String CUsuario;
NombreUsuario = getIntent().getExtras().getString(CUsuario);

To solve it you should use a constant name so that you can find the value that you are passing from the MainActivity. Remaining the code as follows:

MainActivity :

siguiente.putExtra("usuario", euser.getText().toString());

Main2Activity :

NombreUsuario = getIntent().getExtras().getString("usuario");
    
answered by 10.11.2016 в 12:20