Function result JAVA Android

Asked

Viewed 47 times

0

I have a simple question for those who already understand Java but this breaking my head.
Being direct, I have my Mainactivity and a layout with two Edittext’s and a button, and a Systemshttp Class.
I wanted that when I clicked on the button, the values of the fields went to the Systemshttp, checked the user and password and showed the return in Mainactivity.
The problem is that since I’m a beginner in Java I can’t do this, but I already have a function base (almost) working:

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);

        }
    });

Systemshttp:

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);
}

1 answer

2


The apparent error is only in the Systemshttp class:

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

No instance of the Mainactivity class is required 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 make the call on click, it will pass the result! For 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 must be like this :

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

if(“OK”.equals(vaido)){
//esta OK
}else{
// nao esta OK
}
  • Caraca old, thanks anyway, Voce explaining is so simple but as I am beginner in this language was more lost than blind and shooting kk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.