如何将变量从线程传递到外部环境?

时间:2012-09-12 13:08:56

标签: android multithreading variables

我在主要活动中嵌套了一个线程:

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

}

我不确定这种结构是否正确。我应该如何将 myInt 变量传递给MainActivity,以便它可以在线程外识别并可操作?

3 个答案:

答案 0 :(得分:0)

更改

public class MainActivity extends Activity {
    // create activity field/member
    private int myIntValue;
    public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);

    runOnUiThread(new Runnable(){
        public void run() {
            myIntValue = 1;
            // Code below works fine and shows me myInt
            TextView textView = (TextView) findViewById(R.id.text_view);
            if (textView != null){
                textView.setText(myInt);
            }
        }
    });

    TextView textView = (TextView) findViewById(R.id.text_view);
    if (textView != null){
        textView.setText(myIntValue);
    }
}

答案 1 :(得分:0)

在尝试设置线程外部的textview(在主线程上)之前,首先需要一个已设置的整数的全局变量。它需要事先设置,因为你启动的新线程将只是启动并移动到下一行代码,所以myInt还没有设置。

然后,至少在最初时,在主线程上使用预定的全局整数值作为textview。如果你想在你开始的线程中更改它,那么在你的类中创建一个方法,如setIntValue(),它将传递线程中的整数并将全局变量设置为该值。如果您愿意,我可以稍后使用代码示例进行更新。

更新:示例代码

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

注意:如果您只希望能够重置textview,而不是拥有一个全局的可操作变量,我建议将方法更改为只传入新的整数并设置textview,而不是存储全局变量,像这样:

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

如果您执行上述操作,请确保从线程内部调用方法时,请在UI线程上调用它,如下所示:     runOnUiThread(new Runnable()){         public void run(){             //更新UI元素         }     }

答案 2 :(得分:-1)

尝试以下方法:

public class MainActivity extends Activity 
{
    TextView textView;

    public void onCreate(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text_view);
        new Thread(new Runnable()
        {
            public void run() 
            {
                int myInt = 1;
                textView.setText(myInt);
            }
        }).start();

    }