簡體   English   中英

如何使用Listview訪問多個JSONArray?

[英]How to access Multiple JSONArrays with Listview?

我有兩個Listview ,我從服務器獲取兩個JSONArray ,我得到以下響應

[
    [
        {
            "user_status": "1",
            "pooja_name": "Festival Sevas",
            "sess_date": "Mon Nov 30 2015",
            "session_status": "Completed",
            "message": "What ever message you want"
        }
    ],
    [
        {
            "user_status": "1",
            "pooja_name": "Pushpalankara Seva",
            "sess_date": "Tue Dec 15 2015",
            "session_status": "Pending",
            "message": "What ever message you want"
        }
    ]
]

我能夠解析這兩個數組,但是在我的兩個listview它都顯示Pushpalankara Seva,我要在第一個列表視圖中顯示的是我想顯示Pushpalankara Seva和第二個Festival Sevas。

class LoadPoojas extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AboutUsFragment.this.getActivity());
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(true);
            pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response
            ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
            ArrayList<HashMap<String,String>> upcomingdata = new ArrayList<HashMap<String, String>>();
            String jsonStr = sh.makeServiceCall(POOJA_LISTING_URL, ServiceHandler.GET);
            map = new HashMap<String, String>();
            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONArray jsonObj = new JSONArray(jsonStr);

                    // Getting JSON Array node

                    JSONArray pastarray = jsonObj.getJSONArray(0);
                    for (int i = 0; i < pastarray.length(); i++) {
                        JSONObject c = pastarray.getJSONObject(i);
                        // creating new HashMap

                        // adding each child node to HashMap key => value
                        map.put(POOJA_LISTING_NAME, c.getString(POOJA_LISTING_NAME));


                    }


                    JSONArray upcoming = jsonObj.getJSONArray(1);
                    for (int i = 0; i < upcoming.length(); i++) {
                        JSONObject c = upcoming.getJSONObject(i);
                        // creating new HashMap
                      //  HashMap<String, String> upcomingmap = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                        map.put(POOJA_LISTING_NAME, c.getString(POOJA_LISTING_NAME));



                    }
                    data.add(map);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return data;
        }
        protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
            super.onPostExecute(result);
            /*if(interestaccept == null || interestaccept.length() == 0){
                // Toast.makeText(getApplicationContext(), "No response", Toast.LENGTH_SHORT).show();
                noacpt.setText("  No Accepted List  ");
            }
            else
            {
                noacpt.setVisibility(View.INVISIBLE);
            }*/
            // dismiss the dialog after getting all albums
            if (pDialog.isShowing())
                pDialog.dismiss();
            // updating UI from Background Thread

            aList = new ArrayList<HashMap<String, String>>();
            aList.addAll(result);
            adapter = new CustomAdapterPooja(getActivity(),result);
            completedpooja.setAdapter(adapter);
            adapterupcoming = new CustomAdapterPoojaUpcoming(getActivity(),result);
            notcompletedpooja.setAdapter(adapterupcoming);
            adapter.notifyDataSetChanged();
            adapterupcoming.notifyDataSetChanged();



            completedpooja.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                }
            });


           /* upcomingaList = new ArrayList<HashMap<String, String>>();
            upcomingaList.addAll(result);
            adapterupcoming = new CustomAdapterPoojaUpcoming(getActivity(),result);
            notcompletedpooja.setAdapter(adapterupcoming);

            adapterupcoming.notifyDataSetChanged();

            notcompletedpooja.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                }
            });*/

        }

    }

我檢查了您的代碼,發現在兩個數組中的相同鍵 (在兩個For循環內)中的哈希映射中確實出錯。 因此,它按照哈希圖規則用最新值覆蓋。

解決方案

選項1:獲取hashmap的arraylist並為每個新記錄創建新的hashmap。

選項2:獲取POJO類的arraylist。

編輯:

public class UserPOJO {


    public String user_status;
    public String pooja_name;
    public String sess_date;
    public String session_status;
    public String message;

}

如下所示獲取POJO類的arraylist

public ArrayList<UserPOJO> userPOJOs = new ArrayList<UserPOJO>();

現在,將數據從JSONArray插入arraylist中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM