Result of JAVA Android function

0

I have a very simple question for anyone who already understands Java but it's breaking my head. Being direct, I have my MainActivity and a layout with two EditText's and a button, and a SystemHttp Class.
I wanted when I clicked on the button, the values of the fields went to SystemHttp, check the username and password and show the return in MainActivity.
The problem is that since I'm a Java inicator I can not do this, but I already have a (almost) function basis:

MainActivity:

    txt_usuario = (EditText) findViewById(R.id.usuario);
    txt_senha = (EditText) findViewById(R.id.senha);
    btn_login = (Button) findViewById(R.id.btn_login);

    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String user = txt_usuario.getText().toString();
            String pssw = txt_senha.getText().toString();

            SistemaHttp sHttp = new SistemaHttp(getBaseContext());

            sHttp.retornaUsuario(user, pssw);

        }
    });

SystemHttp:

public String retornaUsuario(String user, String pssw)
{
    String u = "Teste";
    String s = "123";
    String resp = "";

    if (user.equals(u) && pssw.equals(s))
    {
        resp = "OK";
    }
    else
    {
        resp = "ERRO";
    }

    MainActivity main = new MainActivity();
    main.retornoLogin(resp);
}
    
asked by anonymous 16.03.2016 / 20:04

1 answer

2

The apparent error is only in the SystemHttp class:

MainActivity main = new MainActivity();
main.retornoLogin(resp);

You do not need to instantiate the MainActivity class to return, who will do this is the method itself, because it returns a String, right?

public String retornaUsuario(String user, String pass)

So when you perform the call in the click, it will pass the result! To do this you must change the method to the following form:

public String retornaUsuario(String user, String pssw)
{
    String u = "Teste";
    String s = "123";
    String resp = "";

    if (user.equals(u) && pssw.equals(s))
    {
        resp = "OK";
    }
    else
    {
        resp = "ERRO";
    }
// passamos a quem solicitou o retorno !
  return resp;
}

Your call should look like this:

SistemaHttp sHttp = new SistemaHttp(getBaseContext());
String valido = sHttp.retornaUsuario(user, psst);

if(“OK”.equals(vaido)){
//esta OK
}else{
// nao esta OK
}
    
16.03.2016 / 21:11