2017-01-03 31 views
0

我想要在後臺運行一個asynctask,另一個從web服務加載一些東西來獲得新的列表視圖。同時運行asyntask(android)

這是我的代碼。

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循環就不會照我所選擇的複選框的數量環。請幫忙。

+0

爲什麼你發佈所有的代碼和所有有關「異步」 AsyncTasks解釋,如果你實際的問題是有關檢查'ListView'或'RecyclerView'項目?發佈您的列表適配器,以及如何更改您的'lvList'中的項目以使它們成爲'isSelected'。 – Divers

+0

你可以在後臺運行一個任務而不顯示加載程序,在UI線程中... –

+0

@Divers因爲我確認我的複選框沒有問題。現在的問題是asynctask的for循環。它不是根據我選擇的複選框的數量循環。 –

回答

0

did 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; 
+0

@Mahdouani是的。列表大小是正確的,我只是想知道爲什麼for循環不循環 –