How to change a value in another class?

Asked

Viewed 54 times

1

I’m trying to change the value of a variable in another class but I’m not getting it.

package com.cursoandroid.firebaseapp.novotestebanco;

public  class Logado {

   public String situacao;


    Logado(String situacao){
        this.situacao = "deslogado";
    }

    public  String getSituacao() {
        return situacao;
    }

    public void setSituacao(String situacao) {
        this.situacao = situacao;
    }
}




botaoMostrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Logado logado = new Logado("");
            logado.setSituacao("logado");
            mostrar.setText(logado.getSituacao());
            startActivity(new Intent(MainActivity.this, Mostrar.class));



        }
    });

Class Show

public class Mostrar extends AppCompatActivity {
    public TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mostrar);

        text = (TextView) findViewById(R.id.texto);

        Logado logado = new Logado("");
        text.setText(logado.getSituacao());
    }
}
  • Can you give more details? Which variable do you want to change? What happens when you try to run your program? Do you have an error? It’ll make it easier for the staff to answer you.

  • Note that in the constructor Logado(String situacao) the situation parameter is useless since you initialize the attribute with a fixed value this.situacao = "deslogado".

  • put the variable as Static and it worked, vlw same personal by the help.

No answers

Browser other questions tagged

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