android - 在AsyncTask中获取IndexOutOfBoundException

我有一堂课。它具有CheckListcheckListIdtitle

我有2个asyncTasks。一个用于CheckListItemArrayList,另一个用于GetCheckLists。在GetCheckListItems类中,它具有CheckListcheckListIdTitle个。

首先,我使用arrayList获取所有清单。现在,对于每个清单,我正在调用checkListItems任务以获取所有GetCheckListAsyncTask

现在GetCheckListItemsAsynccheckListItems方法我想将onPostExecute设置为GetCheckListItemsAsyncTask的对象。

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

    }
}


为了获得checkListItemArrayList的位置,我创建了一个迭代计数变量,并用CheckListcheckListArrayList方法隐含了该变量。并尝试在GeckCheckListAsyncTask中访问它,并在checkList对象中设置arraylist。

但是如果postExecute的循环,则会使索引超出绑定异常。如何获取GetCheckListItemsAsyncTask的索引以将GetCheckListItemAsyncTask设置为与checkListArray相同的位置?

日志:

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)

最佳答案

改成:

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

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


您正在将i0迭代到iterationCount。但是,在内部循环中,您始终使用iterationCount而不是i

本文翻译自 https://stackoverflow.com/questions/38211330/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 android arraylist android-asynctask position indexoutofboundsexception


相关文章:

android - 使用contentprovider在sqlite android中插入一条记录

android - Android - 更改READ_PHONE_STATE权限请求消息

java - 遍历ArrayList以在第二个Activity Java中显示在多行ListView中?

java - 在ArrayList中找到最大值

android - 如果从主线程执行方法,如何抛出异常

android - Android文件复制导致java.io.IOException:文件描述符已关闭

android - 调用需要API级别21(当前为14)

android - 无法运行程序“xx / sdk // tools / emulator”:java.io.IOException:error = 2,没有这样的文件或目录

java - 将地图转换为列表

java - 是否有比ArrayList的O(n)更好的搜索方法?