如何在asyncTask的postExecute之后将arraylist设置为object?

时间:2016-07-05 13:22:28

标签: java android arraylist android-asynctask

我有2个asyncTasks。一个用于GetCheckLists另一个用于GetCheckListItems。 在CheckList类中,它具有checkListId,Title等,以及checkListItems的arrayList。

首先,我使用GetCheckListAsyncTask获取所有检查列表。现在对于每个checkList,我调用GetCheckListItemsAsync任务来获取所有checkListItems。

现在,GetCheckListItemsAsyncTask的onPostExecute方法我想设置checkListItemArrayList。

如何确保将checkListItemArrayList添加到checkList项目的对象?

CheckListActivity:

    public class CheckListActivity extends AppCompatActivity implements CheckListAdapter.OnItemClickListener{

    private ProgressDialog progressDialog;
    private RecyclerView recyclerView;
    private ArrayList<CheckList> checkLists = new ArrayList<>();
    private CheckList mCheckList;
    private ArrayList<CheckListItem> itemList;
    private ArrayList<CheckList> checkListArrayList;
    private CheckListAdapter mAdapter;
    JSONArray checkListsItemArray,checkListArray;
    public int iterationCount = 0;

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

        checkListArrayList = 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);

    }

    }

    @Override

    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
                        String checkListItemBudget = subObject.getString("budget");//name of the attribute in response
                        String checkListItemTimedate = subObject.getString("time_due");
                        String checkListItemReminder = subObject.getString("reminder");
                        String checkListItemId = subObject.getString("checklistItemId");
                        String checkListItemStatus = subObject.getString("status");

                        if (checkListItemStatus.equals("1")) {
                            completedTasks++;
                        }
                        if (checkListItemStatus.equals("0")) {
                            pendingTasks++;
                        }

                        checkListItem.setTitle(checkListItemName);
                        checkListItem.setBudget(checkListItemBudget);
                        checkListItem.setDateTime(checkListItemTimedate);
                        checkListItem.setReminder(checkListItemReminder);
                        checkListItem.setCheckListItemId(checkListItemId);
                        checkListItem.setStatus(checkListItemStatus);
                        checkListItem.setPendingItem(pendingTasks);
                        checkListItem.setCompletedItem(completedTasks);

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

                    if(checkListArrayList.size() < iterationCount) {
                        iterationCount++;
                        String checkListId =
                                checkListArrayList.get(iterationCount).getCheckListId();
                        CheckList checkList1 = checkListArrayList.get(iterationCount);
                        checkList1.setCheckListItemArrayList(itemList);

                    }

                    mAdapter.notifyDataSetChanged();

                }
                else {
                    Toast.makeText(CheckListActivity.this, message, Toast.LENGTH_LONG).show();
                    //code after failed getting profile details goes here
                }
            } catch(JSONException je) {
                je.printStackTrace();
                Toast.makeText(CheckListActivity.this, je.getMessage(), Toast.LENGTH_LONG).show();
            }
        } //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
                        String checkListBudget = subObject.getString("budget");//name of the attribute in response
                        String checkListIcon = subObject.getString("icon");
                        String checkListId = subObject.getString("checklistId");

                        checkList.setCheckListTitle(checkListName);
                        checkList.setBudget(checkListBudget);
                        checkList.setImageIcon(checkListIcon);
                        checkList.setCheckListId(checkListId);

                        checkListArrayList.add(checkList);

                        iterationCount++;
                        new GetCheckListsItemAsyncTask().execute(checkListId);

                        mAdapter.notifyDataSetChanged();
                    }

                    if ((progressDialog != null) && progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                } 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) {
                je.printStackTrace();
                Toast.makeText(CheckListActivity.this, je.getMessage(), Toast.LENGTH_LONG).show();
            }
        } //end of onPostExecute
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            progressDialog.setMessage("Loading...");
            progressDialog.show();
        }

    }
}

如何将CheckListItemsArrayList顺序设置为checkListArrayList的对象?请帮忙。谢谢..

2 个答案:

答案 0 :(得分:0)

您需要详细说明您的问题。这非常令人困惑。

但我认为你想要在AsyncTask类中添加项目。

您可以使用构造方法

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

AsyncTask 只需添加:

JSONArray m_checkListsItemArray;

public GetCheckListsItemAsyncTask(JSONArray checkListsItemArray){
       m_checkListsItemArray = checkListsItemArray; 
       //Do something here with checkListsItemArray;
}

在AsycTask类的任何位置使用m_checkListsItemArray。

答案 1 :(得分:0)

每次启动任务时,您都无法控制结束。任务以异步方式运行,因此它们不会以您启动它们的顺序结束。也许有一个字段级Array或ArrayList,每次任务结束时都会添加结果,然后当每个任务结束时,你都可以使用数组结果。