更新UI线程的AsyncTask在公共类 [英] Updating UI Thread from AsyncTask in public class

查看:163
本文介绍了更新UI线程的AsyncTask在公共类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些的AsyncTask类剥离到公共(单独)功能,因此我并没有改写了这么多code。我几乎有它,除了一个非常重要的方面。该AsyncTask的函数编译一个ArrayList通过PHP调用服务器。当这个名单是完整的,我需要更新主UI线程上的微调。我发现了一个非常好的答案<一href="http://stackoverflow.com/questions/13815807/return-value-from-asynctask-class-onpostexecute-method">here但我有一点点困难,使其正常工作。

下面是什么,我有一个缩小版本:(请注意,在这一点上,所有我想要做的是叫吐司消息证明往返正)

下面是调用活动

 公共类MyActivity扩展活动实现OnTaskCompleted {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        sg_list =新的ArrayList&LT;字符串&GT;();
        新GetSuperGroups(UpdateAffiliations preferences.this,语境,检索组列表...)执行()。
    }

    公共无效onTaskCompleted(ArrayList中&LT;字符串&GT;列表){
        Toast.makeText(getApplicationContext(),你好,从异步,Toast.LENGTH_SHORT).show();
    }

}
 

这是界面:

 公共接口OnTaskCompleted {
    无效onTaskCompleted(ArrayList中&LT;字符串&GT;清单);
}
 

最后,这里是的AsyncTask 。请注意,这是一个公共类:

 公共类GetSuperGroups延伸的AsyncTask&LT;字符串,字符串,ArrayList的&LT;字符串&GT;&GT; {

    私人活动的活动;
    私人上下文的背景下;
    私人字符串progressMsg;
    私人ProgressDialog pDialog;
    私人的ArrayList&LT;字符串&GT; sg_list;
    私人OnTaskCompleted监听;

    公共GetSuperGroups(活动活动,上下文的背景下,字符串progressMsg){
        this.activity =活动;
        this.context =背景;
        this.progressMsg = progressMsg;
    }

    公共无效setSuperGroupList(OnTaskCompleted监听器){
        this.listener =侦听器;
    }

    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(上下文);
        pDialog.setMessage(progressMsg);
        pDialog.setIndeterminate(假);
        pDialog.setCancelable(真正的);
        pDialog.show();
    }

    @覆盖
    受保护的ArrayList&LT;字符串&GT; doInBackground(字符串参数... args){

        sg_list =新的ArrayList&LT;字符串&GT;();
        //使PHP调用,编译的ArrayList
        返回sg_list;
    }

    保护无效onPostExecute(ArrayList中&LT;字符串&GT; sg_list){
        pDialog.dismiss();
        //这下一行导致空指针错误
        //注意,我扔掉了数组列表现在
        //我想要做的就是证明我可以调用吐司回调用活动
        listener.onTaskCompleted(新的ArrayList&其中;字符串&GT;());
    }
}
 

解决方案

就在你的的AsyncTask OnTaskCompleted监听器参数> 构造 GetSuperGroups 。然后通过当您执行的AsyncTask

 公共类MyActivity扩展活动实现OnTaskCompleted {

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            sg_list =新的ArrayList&LT;字符串&GT;();
            新GetSuperGroups(UpdateAffiliations preferences.this,语境,检索组列表...,这一点).execute();
        }

        公共无效onTaskCompleted(ArrayList中&LT;字符串&GT;列表){
            Toast.makeText(getApplicationContext(),你好,从异步,Toast.LENGTH_SHORT).show();
        }

    }
 

 公共类GetSuperGroups延伸的AsyncTask&LT;字符串,字符串,ArrayList的&LT;字符串&GT;&GT; {

        私人活动的活动;
        私人上下文的背景下;
        私人字符串progressMsg;
        私人ProgressDialog pDialog;
        私人的ArrayList&LT;字符串&GT; sg_list;
        私人OnTaskCompleted监听;

        公共GetSuperGroups(活动活动,上下文的背景下,字符串progressMsg,OnTaskCompleted监听器){
            this.activity =活动;
            this.context =背景;
            this.progressMsg = progressMsg;
            this.listener =侦听器;
        }
 

I'm trying to split off some AsyncTask classes into public (separate) functions so that I'm not having to rewrite so much code. I almost have it, except for one very important aspect. The AsyncTask function compiles an ArrayList by making php calls to a server. When this list is complete, I need to update a spinner on the main UI thread. I found a really nice answer here but I'm having a little difficulty making it work.

Here is a scaled down version of what I have: (note that at this point, all I am trying to do is to call a Toast message to prove that the round trip is working)

Here is the calling Activity:

public class MyActivity extends Activity implements OnTaskCompleted {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sg_list = new ArrayList<String>();
        new GetSuperGroups(UpdateAffiliationsPreferences.this, context, "Retrieving Group List...").execute();
    }

    public void onTaskCompleted(ArrayList<String> list) {
        Toast.makeText(getApplicationContext(), "hello from async", Toast.LENGTH_SHORT).show();
    }

}

This is the interface:

public interface OnTaskCompleted {
    void onTaskCompleted(ArrayList<String> list);
}

And finally, here is the AsyncTask. Note that it is a Public class:

public class GetSuperGroups extends AsyncTask<String, String, ArrayList<String>> {

    private Activity activity;
    private Context context;
    private String progressMsg;
    private ProgressDialog pDialog;
    private ArrayList<String> sg_list;
    private OnTaskCompleted listener;

    public GetSuperGroups(Activity activity, Context context, String progressMsg) {
        this.activity = activity;
        this.context = context;
        this.progressMsg = progressMsg;
    }

    public void setSuperGroupList (OnTaskCompleted listener){
        this.listener = listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage(progressMsg);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected ArrayList<String> doInBackground(String... args) {

        sg_list = new ArrayList<String>();
        //make a php call, compile the ArrayList    
        return sg_list;
    }

    protected void onPostExecute(ArrayList<String> sg_list) {
        pDialog.dismiss();
        //this next line causes a null pointer error
        //note that I am throwing away the array list for now
        //all I want to do is prove that I can call the Toast back in the calling Activity
        listener.onTaskCompleted(new ArrayList<String>());
    }
}

解决方案

Just add OnTaskCompleted listener parameter in your asyncTask constructor GetSuperGroups. Then pass this when you execute your asyncTask.

public class MyActivity extends Activity implements OnTaskCompleted {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            sg_list = new ArrayList<String>();
            new GetSuperGroups(UpdateAffiliationsPreferences.this, context, "Retrieving Group List...", this).execute();
        }

        public void onTaskCompleted(ArrayList<String> list) {
            Toast.makeText(getApplicationContext(), "hello from async", Toast.LENGTH_SHORT).show();
        }

    }

and

  public class GetSuperGroups extends AsyncTask<String, String, ArrayList<String>> {

        private Activity activity;
        private Context context;
        private String progressMsg;
        private ProgressDialog pDialog;
        private ArrayList<String> sg_list;
        private OnTaskCompleted listener;

        public GetSuperGroups(Activity activity, Context context, String progressMsg, OnTaskCompleted listener) {
            this.activity = activity;
            this.context = context;
            this.progressMsg = progressMsg;
            this.listener = listener;
        }

这篇关于更新UI线程的AsyncTask在公共类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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