Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables from the thread to the outside environment?

I have a Thread nested in the main activity:

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);

    new Thread(new Runnable(){
        public void run() {
            int myInt = 1;
            // Code below works fine and shows me myInt
            TextView textView = (TextView) findViewById(R.id.text_view);
            textView.setText(myInt);
        }
    }).start();

    // Code below doesn't work at all
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(myInt);

}

I'm not sure if this structure is correct at all. How should I pass myInt variable to the MainActivity so it will become recognizable and operable outside the Thread?

like image 718
LoomyBear Avatar asked Nov 27 '22 10:11

LoomyBear


1 Answers

You would first want a global variable for the integer that is already set before you try setting the textview that's outside of your thread (on the main thread). It needs to be set beforehand because the new thread you start will simply be started and move along to the next lines of code, so myInt won't have been set yet.

Then, use the predetermined global integer value for the textview on the main thread, at least initially. If you would like to change it from within the thread you started, then create a method in your class like setIntValue() which will pass in the integer from the thread and set the global variable to that value. I can update with code examples later if you'd like.

UPDATE: example code

public class MainActivity extends Activity {

//your global int
int myInt

public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

new Thread(new Runnable(){
    public void run() {
        int myRunnableInt = 1;
        // Code below works fine and shows me myInt
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText(String.valueOf(myRunnableInt));

        //say you modified myRunnableInt and want the global int to reflect that...
        setMyInt(myRunnableInt);
    }
}).start();

//go ahead and initialize the global one here because you can't directly access your
myRunnableInt
myInt = 1;

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(String.valueOf(myInt)); //now you will have a value here to use

//method to set the global int value
private void setMyInt(int value){
    myInt = value;

    //you could also reset the textview here with the new value if you'd like
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(String.valueOf(myInt));
}
}

Note: If you only wish to be able to reset the textview, as opposed to having a global, operable variable, I would suggest changing the method to just pass in the new integer and set the textview, rather than storing a global variable, like this:

private void setTextView(int newInt){
    TextView textView = (TextView) findViewById(R.id.text_view);
    textView.setText(String.valueOf(newInt));
}

If you do the above, make sure when you call the method from within the thread, call it on the UI thread like so: runOnUiThread(new Runnable()){ public void run(){ //update UI elements } }

like image 73
dennisdrew Avatar answered Dec 06 '22 18:12

dennisdrew