在 AsyncTask 中获取所需数据后,我在刷新 TextView 时遇到问题。如果它在单个文件中,一切都会很好,但我决定将其拆分。在第一个类中,我有 TextViews,在另一个类中,我有一些从网络下载数据的 AsyncTasks。 我已经尝试过的:
主要 Activity :
summary.countCash(); // it runs AsyncTask in summary class
while (summary.wait) {
// do nothing and wait until AsyncTask finishes
}
eCash.setText("Cash: " + summary.cash);
在 AsyncTask 中:
Boolean wait = false;
@Override
protected void onPreExecute() {
wait = true;
}
@Override
protected void onPostExecute(Integer result) {
cash = result;
wait = false;
}
不幸的是,它不起作用。应用程序卡住。 我也尝试过设置
wait = false;
在 doInBackground 结束时,但随后在 main 中我得到了summary.cash = 0,这可能是因为它在 onPostExecute 中更新值之前刷新了 TextView。
最佳答案
在您的异步任务中。
@Override
protected void onPostExecute(Integer result) {
eCash.setText("Cash: " + result);
}
去掉 while 语句。你不需要它。 仅供引用,您通过等待锁定了 ui 线程,这首先就违背了 asynctask 的目的。
希望这有帮助
关于java - 执行AsyncTask后刷新TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21967037/