6

我正在尝试将一些 AsyncTask 类拆分为公共(单独的)函数,这样我就不必重写这么多代码。我几乎拥有它,除了一个非常重要的方面。AsyncTask 函数通过对服务器进行 php 调用来编译 ArrayList。完成此列表后,我需要更新主 UI 线程上的微调器。我在这里找到了一个非常好的答案,但我很难让它发挥作用。

这是我所拥有的缩小版本:(请注意,此时,我要做的只是调用一条Toast消息来证明往返行程有效)

这是调用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();
    }

}

这是界面:

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

最后,这是AsyncTask. 请注意,它是一个公共类:

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>());
    }
}
4

4 回答 4

7

只需OnTaskCompleted listener在您的asyncTask constructor GetSuperGroups. 然后this在你执行你的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();
        }

    }

  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;
        }
于 2013-08-28T05:11:05.420 回答
2

你永远不会打电话setSuperGroupList(),所以listener仍然是空的。最好将侦听器放入任务的构造函数中。

于 2013-08-28T04:27:44.810 回答
0

你走的是正确的道路。但我不清楚 onPostExecute(..) 方法中的问题。是关于 null pDialog 还是 null sg_list。如果 sg_list 有问题,请检查 php 方法是否有效?清单文件 foe php 服务中的权限可能有问题。
所以我的建议是首先在 UI 线程上检查你的方法。然后将其更改为在后台线程中执行。
您也可以将 toast 放在 onPostExecute 方法中以检查 sg_list 内容。

于 2013-08-28T04:56:05.677 回答
0

您需要添加一个函数 setListener:

public class TaskComplete {
    public OnTaskCompleted onTaskListenerComplete;

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

    public void setOnTaskComplete(OnTaskCompleted onTaskListenerComplete){
        this.onTaskListenerComplete = onTaskListenerComplete;
    }
}

在 Activity 中,定义并设置它:

public class MyActivity extends Activity implements OnTaskCompleted {
    public TaskComplete task;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sg_list = new ArrayList<String>();
        task.setOnTaskComplete(this);
        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();
    }

}

在异步任务中:

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
    activity.onTaskCompleted(new ArrayList<String>());
}

希望它有用。

于 2013-08-28T05:02:43.930 回答