我目前正在研究我的第一個Android應用程序。如何修改UI線程中聲明的變量,從其他線程?
應用程序訪問數據庫以獲取我想要在屏幕上打印的一些信息。要發送請求並在網絡上獲得答案,我需要使用與UI線程不同的新線程(我將其命名爲「N線程」)。這部分是好的。
現在,我想修改變量eventList以獲取存儲在N線程的集合中的值。
public class MainActivity extends AppCompatActivity {
public List<Event> eventList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* I fill the list in an other thread */
new Thread(new Runnable() {
public void run(){
eventList = new WebService().getEvents(); //returns a list
}
// if I check here, eventList contains elements
}).start();
/* I check the result */
TextView respView = (TextView) findViewById(R.id.responseView);
if(eventList != null)
{
respView.setText("Ok");
} else {
respView.setText("Not ok");
}
...
}
的問題是:EVENTLIST不被修改。如何修改這個變量並從UI線程打印它? 謝謝你的幫助。
變量不屬於任何線程。你不應該做你所做的事,而應該看看https://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask(ps:它*是*修改過,但是這種改變沒有發生了) – zapl