How to exchange information between classes?

Asked

Viewed 2,626 times

0

I want to pass information from one class to another for example:

TextView t;

I have a Textview here called t and he’s in the Main class.

Now in the Main2 class:

t.setText("TextView saiu da Main para a Main2");

I already tried to put public but still says that the variable t is not existing in Main2.

  • Have you tried to put in the statement of f: public static float f = 3.5;?

  • Yes I’ve tried thanks

  • Gutie, I’m reversing the edits here. Let’s focus on the floats problem in the other question ok? Please edit the other one focusing on this problem.

  • Look at my code @bfavaretto see what you can do: https://github.com/AndroidComunite/Android-Error/tree/master

  • @Gutie Who has to do it is you... There in the other question.

3 answers

4

To access a variable of another class, simply indicate that the variable is estática, which will allow you to access it without the need for an object:

public static TextView t;

To access from another class, you must also specify the class in which the variable exists:

Main.t.setText("TextView saiu da Main para a Main2");

example:

public class Example{
    public static TextView t;
}
public class Example2{
    public static void main(String[] args){
        Example.t.setText("TextView saiu da Main para a Main2");
    }
}

2

May I suggest 3 options

1 Option

Use Singleton, or simply set a class to Static.

public class Singleton {

    private static Singleton uniqueInstance;

    private Singleton() {
    }

    public static synchronized Singleton getInstance() {
        if (uniqueInstance == null)
            uniqueInstance = new Singleton();
        return uniqueInstance;
    }
}

Read more in: Singleton Java Project Pattern http://www.devmedia.com.br/padrao-de-projeto-singleton-em-java/26392#ixzz3FfAWD3Il

2 option

Set variables or global objects, you can create a Application class on Android and set global variables or even Singleton.

Example.

3 Option

You can pass variables or objects by Intent to other activitys or Fragments

Activity 1

Intent i=new Intent("....");
i.putExtra("TEXT", "Olá");
startActivity(i);

Activity 2

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

Bundle bundle = getIntent().getExtras();

String var_intent = bundle.getString("TEXT");
text .setText(var_intent );

Read more.

0

You can use the following code below:

Main1 class:

import android.widget.TextView;

public class Main1 {

    private TextView t;

    public TextView getT() {
        return t;
    }

}

Main2 class:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2 extends Activity {

    private TextView t;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Main1 objeto = new Main1();
        t = objeto.getT();
        t = (TextView) findViewById(R.id.textView1); //aqui você põem o nome do id do textView
        t.setText("TextView saiu da Main para a Main2");

    }

}
  • The class instance is local, it will not work unless it is Static. and its access also.

Browser other questions tagged

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