1

Ich habe eine Klasse von CheckList. Es hat checkListId, title und auch eine CheckListItemArrayList.Abrufen von IndexOutOfBoundException in AsyncTask

Ich habe 2 asyncTasks. Eine für GetCheckLists andere für GetCheckListItems. In CheckList Klasse hat es checkListId, Title und arrayList von checkListItems.

Zuerst bekomme ich alle Checklisten mit GetCheckListAsyncTask. Jetzt rufe ich für jede checkList GetCheckListItemsAsync Aufgabe an, um alle checkListItems zu erhalten.

Jetzt onPostExecute Methode von GetCheckListItemsAsyncTask Ich möchte die checkListItemArrayList auf das Objekt CheckList setzen.

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 

    } 
} 

Um die Position checkListArrayList bekomme ich erstellt eine Iteration zählen Variable und increamented es in GeckCheckListAsyncTask ‚s postExecute Methode. und versuchte in GetCheckListItemsAsyncTask darauf zuzugreifen und die arraylist im Objekt der checkList zu setzen.

Aber es ist Index Out-of-Bound-Ausnahme werfen, wenn for-Schleife von GetCheckListItemAsyncTask. Wie sollte ich den Index checkListArray erhalten, um die CheckListItemArray auf die gleiche Position von checkListArrayList zu setzen?

Log:

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

Bitte schreiben Sie das Fehlerprotokoll – Shank

+0

hinzugefügt das Protokoll. Bitte helfen .. Danke .. @ Shank – Sid

+0

welche Zeilen sind '336' und' 261' – Shank

Antwort

1

Wechseln zu:

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

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

Du i0-iterationCount iterieren. Allerdings verwenden Sie immer innerhalb der Schleife iterationCount und nicht i

+0

ja .. danke, dass es die Ausnahme gelöst .. Aber ich bekomme nur zuletzt generierte ItemList für jede Checkliste. Könntest du bitte dabei helfen? @Guilherme P – Sid

0

sollten Sie ändern

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

dieser

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

Sie nur fünf Elemente im Array haben sechs nicht, weshalb Sie den Absturz zu bekommen.

+0

versuchte dies, trotzdem bekomme ich die Ausnahme. @ apmartin1991 – Sid

+0

Welche Zeile wirft den Fehler? – apmartin1991

+0

zwei Zeilen innerhalb der for-Schleife von GetCheckListItemsAsyncTask. @ apmartin1991 – Sid