简体   繁体   English

执行AsyncTask后刷新TextView

[英]Refresh TextView after executing AsyncTask

I have problem with refreshing TextView after I get required data in AsyncTask. 在AsyncTask中获取所需的数据后,我在刷新TextView时遇到问题。 Everything would be fine if it was in single file, but I decided to split it. 如果将其放在单个文件中,一切都会很好,但是我决定将其拆分。 In first class I have TextViews and in another one I have few AsyncTasks which downloads data from net. 在第一堂课中,我有TextViews,在另一堂课中,我有几个AsyncTasks,它们可以从网上下载数据。 What I've already tried: 我已经尝试过的:

mainActivity: 主要活动:

summary.countCash(); // it runs AsyncTask in summary class
while (summary.wait) {
    // do nothing and wait until AsyncTask finishes
}

eCash.setText("Cash: " + summary.cash);

and in AsyncTask: 并在AsyncTask中:

Boolean wait = false;

@Override
protected void onPreExecute() {
    wait = true;
}

@Override
protected void onPostExecute(Integer result) {
    cash = result;
    wait = false;
}

Unfortunately it does not work. 不幸的是,它不起作用。 App freezes. 应用程序冻结。 I also tried to set 我也尝试设定

wait = false;

at the end of doInBackground, but then in main I got summary.cash = 0, which is probably because it refreshes TextView before value is updated in onPostExecute. 在doInBackground的末尾,但主要是得到summary.cash = 0,这可能是因为它在onPostExecute中更新值之前刷新了TextView。

In your async task. 在您的异步任务中。

@Override
protected void onPostExecute(Integer result) {
    eCash.setText("Cash: " + result);
}

Get rid of your while statement. 摆脱您的while语句。 You don't need it. 不用了 Fyi, you are locking up the ui thread by waiting, defeating the purpose of the asynctask in the first place. Fyi,您正在等待等待来锁定ui线程,首先打败了asynctask的目的。

Hope this helps 希望这可以帮助

uhm, i don't advise to keep stuck at a continuous loop. 嗯,我不建议保持连续循环。 It will keep the Activity busy. 它将使活动保持繁忙。 Just invoke a callback method from the AsyncTask class. 只需从AsyncTask类调用回调方法。

in AsyncTask class AsyncTask类中

private _mainActivity;

// constructor
public AsyncTask(MainActivity ma) {
    // passing reference
    _mainActivity = ma;
}

@Override
protected void onPostExecute(Integer result) {
    _mainActivity.updateTextview(result);
}

and then in your MainActivity 然后在您的MainActivity

public void updateTextview(Integer result) {
    // update textview using result
}

By this, your MainActivity wouldn't be stuck in a loop. 这样,您的MainActivity不会陷入循环中。 Moreover, you can add a process dialog in the main activity and stop it in the updateTextview method. 此外,您可以在主活动中添加一个流程对话框,并在updateTextview方法中停止它。

edit 1 编辑1

You can call it by using the AsyncTask class as object. 您可以通过使用AsyncTask类作为对象来调用它。 I don't know the class name, assume it's AsyncTask then use the next in your MainActivity 我不知道类名,假设它是AsyncTask然后在MainActivity使用下一个

AsyncTask at = new AsyncTask(this);
at.execute();

the this points to the MainActivity itself, which will be a reference. this指向MainActivity本身,将作为参考。 The AsyncTask class then can use the reference to invoke a callback method to the updateTextview() method. 然后, AsyncTask类可以使用该引用来调用对updateTextview()方法的回调方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM