我有一个使用带有进度对话框的 asyncTask 执行的方法。执行此方法后,它会更改屏幕上的一些内容 - 微调器和 TextView 。在我的方法中,我用新数据更新它们。我知道该方法运行完美并且正在按预期运行,因为当我按下微调器上的箭头时,新数据就在其中。在方法内部调用了 setText() 和 notifyDataSetChanged() - 这些似乎不会更新屏幕。将 notifyDataSetChanged() 添加到我的异步任务的 onPostExecute() 似乎也没有做任何事情。
知道哪里出了问题吗?
这是我的代码
我的方法()
aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
for(Object tmpObject:objects){
aa.add(tmpObject.getName());
}
spin=(Spinner)findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
aa.notifyDataSetChanged();
txt=(TextView)findViewById(R.id.txt);
txt.setText("Woohoo!");
我的异步任务
class loginTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
pd = ProgressDialog.show(login.this, "Logging you in...", "Please wait", true, false);
}
@Override
protected Void doInBackground(Void... unused) {
myMethod();
return null;
}
protected void onPostExecute(Void unused) {
pd.dismiss();
}
}
为什么进度对话框完成后屏幕不更新?如果我不使用 asyncTask,它会更新得很好。将 notifyDataSetChanged() 添加到 postexecute 也不会做任何事情。
最佳答案
doInBackground 方法不在 UI 线程上,所以如果你想更新 UI,你应该在 onProgressUpdate 或 onPostExecute 中进行。
关于android - 异步任务不更新屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524709/