同时运行asyntask(android)

时间:2017-01-03 13:06:22

标签: android android-asynctask

我正在尝试在后台运行asynctask,而另一个从webservice加载一些东西以获得新的listview。

这是我的代码。

AsyncCallForwardListWS

private class AsyncCallForwardListWS extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(Void... params) {

        //List<Request_model> thisList = newList;
        //List<Tn_Parent> thisList = listDataParent;
        List<Request_model> thisList = lvList;
        System.out.println("there2: " + thisList.size());
        for (int y = 0; y< thisList.size(); y++){
            //Request_model model = thisList.get(y);
            //Tn_Parent model = thisList.get(y);
            Request_model model = thisList.get(y);
            if(model.isSelected()){
                if(action.equals("deny")){
                    //getComment = model.getApproverComment();
                    getComment = model.getApprComments();
                }else
                    getComment = "This request is " + ACTION_MSG + " by " + model.getUser_fullName() + " via mobile app";
                //getComment = "This request is " + ACTION_MSG + " by " + model.getApproverName() + " via mobile app";
                taskActivity_forward = model.getTaskActivity();

                getRequestID = model.getRequestId();
                System.out.println("testing");

                db.addInfo(new Request_model(model.getRequestId()));
                System.out.println("requestid: " + getRequestID);

                ForwardWebService();

            }
        }
        return null;
    }

AsyncCallListWS

private class AsyncCallListWS extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.show();
        progressDialog.setContentView(R.layout.custom_progressbar);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setCancelable(false);
        TextView textView1 = (TextView) progressDialog.findViewById(R.id.textView1);
        Typeface dsr = Typeface.createFromAsset(getActivity().getAssets(), getResources().getString(R.string.droid_sans));
        textView1.setTypeface(dsr);
        System.out.println("here1");

        isThereAnyRequest = false;

        if(lvAdapter!=null) {
            lvAdapter.clear();
            lvAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        //listDataParent = new ArrayList<Tn_Parent>();
        listPending();
        System.out.println("here2");
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dismissLoadingDialog();
        System.out.println("here3");
        selectAll.setChecked(false);

        if(getContext()!=null) {
            lvAdapter = new Tn_ListViewAdapter(getActivity(), lvList, selectAll);
            listView.setAdapter(lvAdapter);

            progressDialog.dismiss();
        }


        if (isThereAnyRequest){

            buttonLayout.setVisibility(View.VISIBLE);
            //selectAll.setVisibility(View.VISIBLE);
            checkBox_layout.setVisibility(View.VISIBLE);
            //textView.setVisibility(View.GONE);
            no_request_noti.setVisibility(View.GONE);
        }

        else{
            buttonLayout.setVisibility(View.INVISIBLE);
            //selectAll.setVisibility(View.INVISIBLE);
            checkBox_layout.setVisibility(View.GONE);
            //textView.setVisibility(View.VISIBLE);
            no_request_noti.setVisibility(View.VISIBLE);
        }


    }
}
}

我在其他地方有这个触发asynctask。

new AsyncCallForwardListWS().execute();
new AsyncCallListWS().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

两个asynctasks确实同时在一起运行但是很奇怪,AsyncCallForwardListWS中的for循环不会根据我选择的复选框的数量循环。请帮忙。

1 个答案:

答案 0 :(得分:0)

做了System.out.println(“there2:”+ thisList.size());显示列表的确切数量? 如果没有尝试

List<Request_model> thisList=new List<Request_model>(); 
thisList.addAll(lvList);
//at post of
List<Request_model> thisList = lvList;