1

我有一類CheckList。它有checkListId,titleCheckListItemArrayList在AsyncTask中獲取IndexOutOfBoundException

我有2個asyncTasks。一個用於GetCheckLists另一個用於GetCheckListItems。在CheckList類中,它具有checkListId,TitlearrayListcheckListItems

首先我使用GetCheckListAsyncTask得到所有的checklist。現在爲每個checkList我打電話GetCheckListItemsAsync任務獲得所有checkListItems

現在onPostExecute方法GetCheckListItemsAsyncTask我想將checkListItemArrayList設置爲CheckList的對象。

CheckListActivity:

public class CheckListActivity extends AppCompatActivity implements CheckListAdapter.OnItemClickListener{ 

    public int iterationCount = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_check_list); 

     mIntent = getIntent(); 

     checkListArrayList = new ArrayList<>(); 

     tempCheckListIdList = new ArrayList<>(); 

     mEventId = mIntent.getStringExtra("eventId"); 

     mCheckList = new CheckList(); 

     progressDialog = new ProgressDialog(CheckListActivity.this); 

     recyclerView = (RecyclerView)findViewById(R.id.recycler_view); 

     mAdapter = new CheckListAdapter(checkListArrayList,CheckListActivity.this,CheckListActivity.this); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(mAdapter); 

     GetCheckListAsyncTask getCheckListAsyncTask = new GetCheckListAsyncTask(); 
     getCheckListAsyncTask.execute(mEventId); 

    } 

    public class GetCheckListsItemAsyncTask extends AsyncTask<String, Void, JSONObject> { 

     private String api; 
     private JSONObject jsonParams; 
     public GetCheckListsItemAsyncTask(){} 

     @Override 
     protected JSONObject doInBackground(String... params) { 
      try { 
       api = getResources().getString(R.string.server_url) + "api/checklist_items/getChecklistItems.php"; 

       jsonParams = new JSONObject(); 
       String checklistId = params[0]; // params[0] is username 
       jsonParams.put("checklistId", checklistId); 

       ServerRequest request = new ServerRequest(api, jsonParams); 
       return request.sendRequest(); 
      } catch(JSONException je) { 
       return Excpetion2JSON.getJSON(je); 
      } 
     } //end of doInBackground 

     @Override 
     protected void onPostExecute(JSONObject response) { 
      super.onPostExecute(response); 
      Log.e("ServerResponse", response.toString()); 
      try { 
       int result = response.getInt("result"); 
       String message = response.getString("message"); 
       if (result == 1) { 
        Toast.makeText(CheckListActivity.this, message, Toast.LENGTH_LONG).show(); 
        //code after getting profile details goes here 
        checkListsItemArray = response.getJSONArray("checklistItems"); 

        for (int i = 0; i < checkListsItemArray.length(); i++) { 

         int pendingTasks = 0,completedTasks = 0; 

         itemList = new ArrayList<>(); 

         CheckListItem checkListItem = new CheckListItem(); 

         JSONObject subObject = checkListsItemArray.getJSONObject(i); 
         String checkListItemName = subObject.getString("text");//name of the attribute in response 

         checkListItem.setTitle(checkListItemName); 
         checkListItem.setBudget(checkListItemBudget); 

         itemList.add(checkListItem);//adding string to arraylist 
        } 

        CheckList checkList = new CheckList(); 

        for(int i = 0; i <= iterationCount; i++) { 

         String checkListId = checkListArrayList.get(iterationCount).getCheckListId(); 

         checkListArrayList.get(iterationCount).setCheckListItemArrayList(itemList); 
        } 

        mAdapter.notifyDataSetChanged(); 

       } 
       else { 

        //code after failed getting profile details goes here 
       } 
      } catch(JSONException je) { 
      } 
     } //end of onPostExecute 
    } 



    public class GetCheckListAsyncTask extends AsyncTask<String, Void, JSONObject> { 

     private String api; 
     private JSONObject jsonParams; 
     public GetCheckListAsyncTask(){} 

     @Override 
     protected JSONObject doInBackground(String... params) { 
      try { 
       api = getResources().getString(R.string.server_url) + "api/checklist/getChecklists.php"; 

       jsonParams = new JSONObject(); 
       String eventId = params[0]; // params[0] is username 
       jsonParams.put("eventId", eventId); 

       ServerRequest request = new ServerRequest(api, jsonParams); 
       return request.sendRequest(); 
      } catch(JSONException je) { 
       return Excpetion2JSON.getJSON(je); 
      } 
     } //end of doInBackground 

     @Override 
     protected void onPostExecute(JSONObject response) { 
      super.onPostExecute(response); 
      //Log.e("ServerResponse", response.toString()); 
      try { 
       int result = response.getInt("result"); 
       String message = response.getString("message"); 
       if (result == 1) { 
        Toast.makeText(CheckListActivity.this, message, Toast.LENGTH_LONG).show(); 
        //code after getting profile details goes here 

        checkListArray = response.getJSONArray("checklists"); 

        for (int i = 0; i < checkListArray.length(); i++) { 
         CheckList checkList = new CheckList(); 
         JSONObject subObject = checkListArray.getJSONObject(i); 
         String checkListName = subObject.getString("checklist");//name of the attribute in response 

         checkList.setCheckListTitle(checkListName); 
         checkList.setBudget(checkListBudget); 

         checkListArrayList.add(checkList); 

         if(checkListArrayList.size() > iterationCount) { 

          String checkListId1 = checkListArrayList.get(iterationCount).getCheckListId(); 
          new GetCheckListsItemAsyncTask().execute(checkListId1); 

          iterationCount ++; 
         } 

         mAdapter.notifyDataSetChanged(); 

        } 
       else { 
        Toast.makeText(CheckListActivity.this, message, Toast.LENGTH_LONG).show(); 
        //code after failed getting profile details goes here 
        if ((progressDialog != null) && progressDialog.isShowing()) { 
         progressDialog.dismiss(); 
        } 
       } 
      } catch(JSONException je) { 
      } 
     } //end of onPostExecute 

    } 
} 

要獲得checkListArrayList位置我創建了一個迭代次數變量和GeckCheckListAsyncTaskpostExecute方法increamented它。並試圖在GetCheckListItemsAsyncTask中訪問它,並在checkList的對象中設置arraylist。

但是,如果對於GetCheckListItemAsyncTask的循環,它將引發索引超出界限的異常。我應該如何獲得指數checkListArrayCheckListItemArray設置爲checkListArrayList的相同位置?

登錄:

Process: com.example.siddhi.meavita, PID: 20503 
    java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5 
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
    at java.util.ArrayList.get(ArrayList.java:308) 
    at com.example.siddhi.meavita.Activities.CheckListActivity$GetCheckListsItemAsyncTask.onPostExecute(CheckListActivity.java:336) 
    at com.example.siddhi.meavita.Activities.CheckListActivity$GetCheckListsItemAsyncTask.onPostExecute(CheckListActivity.java:261) 
+0

請發佈錯誤日誌 – Shank

+0

添加日誌。請幫忙..謝謝.. @ Shank – Sid

+0

什麼行是'336'和'261' – Shank

回答

1

更改爲:

for(int i = 0; i < iterationCount; i++) { 

    String checkListId = checkListArrayList.get(i).getCheckListId(); 
    checkListArrayList.get(i).setCheckListItemArrayList(itemList); 
} 

你迭代i0iterationCount。但是,內部循環你總是使用iterationCount而不是i

+0

是的..謝謝你解決了這個異常..但我只獲取每個checkList最後生成的itemList。你能幫忙嗎? @Guilherme P – Sid

0

你應該改變

for(int i = 0; i <= iterationCount; i++) { 

這個

for(int i = 0; i < iterationCount; i++) { 

你只需要在數組中的五個項目不是6這就是爲什麼你得到的崩潰。

+0

試過了,我仍然得到異常。 @ apmartin1991 – Sid

+0

哪一行會拋出錯誤? – apmartin1991

+0

GetCheckListItemsAsyncTask for循環中的兩行。 @ apmartin1991 – Sid