我想从分离线程访问并填充ListView ...但它的对象在新线程中没有它的作用域...它有什么解决方案?从服务器获取数据并在分离线程中显示listview
ListView FilesListView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FilesListView= (ListView)findViewById(R.id.remoteFilesListView);
new Thread (new Runnable() {
@Override
public void run() {
// fetch data from server…
String xmlFormServer = Worker.getXmlresponse();
Log.d("Response from Serever", xmlFormServer;
// FilesListView object of Listview is not accessable in this thread to populate data…
}
}).start();
}
理想的情况下使用的AsyncTask为您希望在Android中做线程操作。为它优化的IT,并提供UI线程方法来更新您的用户界面。此外,您正在尝试从后台线程更新UI,这会导致您的应用程序崩溃。 –