如何从AsyncTask的回调函数访问父类的看法? [英] How access parent class view from callback function in AsyncTask?

查看:121
本文介绍了如何从AsyncTask的回调函数访问父类的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的示例code。

Here is my sample code.

public class test extends Activity {
  private TextView tv;

  public class Blap extends AsyncTask<String,String,Boolean>{
    private test parent;
    @Override
    protected Boolean doInBackground(String... options) {
        this.auth(options[0],options[1]);
        parent.aresponse(res);
        return true;
    }
    public Blap(test a){
        this.parent = a;
    }
    public auth(String login, String password){
        //process auth
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    //work...
}

public void buttonclick(View v){
    tv = new TextView(this);
    new Blap(this).execute(editText1.getText().toString(),editText2.getText().toString());
    setContentView(tv);
}
public void aresponse(String rs){
    tv.setText(rs);
}
}

但是,当aresponse被调用时,我得到了一个例外:只有创建视图层次可以触摸其观点原来的线程。
我如何正确地等待异步操作完成,并继续处理所需的操作?

But when aresponse is called, I got an exception: only the original thread that created a view hierarchy can touch its views. How do I correctly wait for async operation complete and continue processing needed operations?

推荐答案

的AsyncTask 提供两种posibilites更新UI:

AsyncTask offers you two posibilites to update the UI :

如果您想更新与 doInBackground()(如更新进度),你必须调用 publishProgress() doInBackground()方法内。然后,你必须以更新 onProgressUpdate()法的用户界面。

If you want to update the UI in parallel with the task executed in doInBackground() (e.g. to update a ProgressBar), you'll have to call publishProgress() inside the doInBackground() method. Then you have to update the UI in the onProgressUpdate() method.

如果您想更新UI当任务完成后,你必须这样做,在 onPostExecute()方法。

If you want to update the UI when the task is done, you have to do it in the onPostExecute() method.

/** This method runs on another thread than the UI thread */
@Override
protected String doInBackground(String... _params) {
    for (int progressValue = 0; progressValue  < 100; progressValue++) {
        publishProgress(progressValue);
    }
}

/** This method runs on the UI thread */
@Override
protected void onProgressUpdate(Integer... progressValue) {
    // TODO Update your ProgressBar here
}

/**
 * Called after doInBackground() method
 * This method runs on the UI thread
 */
@Override
protected void onPostExecute(Boolean _result) {
   // TODO Update the UI thread with the final result
}

在你的情况,你应该使用的AsyncTask&LT;字符串,字符串,字符串&GT; ,因为你需要 doInBackground()返回一个字符串。然后,你只需要在治疗 onPostExecute结果()

In your case, you should use an AsyncTask<String, String, String> because you need doInBackground() to return a String. Then you just have to treat the result in onPostExecute()

public class test extends Activity {
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //work...
    }

    public void buttonclick(View v){
        tv = new TextView(this);
        new Blap().execute(editText1.getText().toString(), editText2.getText().toString());
        setContentView(tv);
    }

    public class Blap extends AsyncTask<String, String, String> {
        private test parent;

        @Override
        protected String doInBackground(String... options) {
            this.auth(options[0], options[1]);
            String result = "my result";
            return result;
        }

        public auth(String login, String password){
            //process auth
        }

        @Override
        protected void onPostExecute(String _result) {
           tv.setText(_result);
        }
    }
}

这篇关于如何从AsyncTask的回调函数访问父类的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆