【问题标题】:Last json value is displayed in expandableistview in android最后一个json值显示在android的expandableistview中
【发布时间】:2016-07-01 06:56:35
【问题描述】:

我已经创建了可展开的列表视图。它包含父项和子项。从 Json 中检索父值和子值。子项加载正常,但仅 Json 的最后一个值显示在父项中。如何在 parent 中显示所有项目?

class AsyncT1 extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://abservetechdemo.com/projects/android/food_app/public/mobile/restaurant/resdetails");

            try {

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("res_id","1"));
//                nameValuePairs.add(new BasicNameValuePair("status","veg"));
                nameValuePairs.add(new BasicNameValuePair("main_cat",childname));

                // nameValuePairs.add(new BasicNameValuePair("group_id", group_id));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());


         /* execute */

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                InputStream inputStream = response.getEntity().getContent();
                InputStreamToStringExample str = new InputStreamToStringExample();
                String responseServer = str.getStringFromInputStream(inputStream);
                Log.d("response", "response -----" + responseServer);

                JSONObject jsonRootObject = new JSONObject(responseServer);
                JSONObject jsonobject = jsonRootObject.getJSONObject("restaurants");

                JSONObject questionMark = jsonRootObject.getJSONObject("restaurants");
                Iterator keys = questionMark.keys();
                Log.d("key--", String.valueOf(keys));

                while(keys.hasNext()) {
                    // loop to get the dynamic key
                    String k = (String)keys.next();

                    Group gru = new Group();
                    gru.setName(k);
                    // do something here with the value...
                    final ArrayList<Group> list = new ArrayList<Group>();
                    ArrayList<Child> ch_list;
                    ch_list = new ArrayList<Child>();
                    JSONArray ja = questionMark.getJSONArray(k);


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

                        JSONObject jo = ja.getJSONObject(i);

                        Child ch = new Child();
                        ch.setName(jo.getString("item_name"));
                       ch_list.add(ch);
                    } // for loop end


                    gru.setItems(ch_list);
                    list.add(gru);
                   getActivity(). runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter = new ExpandListAdapter(
                                    getActivity(), list);
                            ExpandList.setAdapter(adapter);

                        }});

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);

        }
    }

【问题讨论】:

    标签: android listview android-asynctask


    【解决方案1】:

    这样做:

    class AsyncT1 extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
    
        //YOUR CODE
    
        try 
        {
            // YOUR CODE
            Log.d("key--", String.valueOf(keys));
    
            // better to initialize main list out side any loop so it will 
            //not get initialized with every iteration of loop..!!
    
            final ArrayList<Group> list = new ArrayList<Group>();
    
            while (keys.hasNext()) {
                // loop to get the dynamic key
                String k = (String) keys.next();
                Group gru = new Group();
                gru.setName(k);
                // do something here with the value...
                ArrayList<Child> ch_list;
                ch_list = new ArrayList<Child>();
                JSONArray ja = questionMark.getJSONArray(k);
    
                for (int i = 0; i < ja.length(); i++) {
    
                    JSONObject jo = ja.getJSONObject(i);
    
                    Child ch = new Child();
                    ch.setName(jo.getString("item_name"));
                    ch_list.add(ch);
                } // for loop end
                gru.setItems(ch_list);
                list.add(gru);
            }
            // set the adapter after the loop has finished the execution
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter = new ExpandListAdapter(
                            getActivity(), list);
                    ExpandList.setAdapter(adapter);
                }
            });
    
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    @Override
    protected void onPostExecute(String aVoid) {
        super.onPostExecute(aVoid);
    
    }
    }
    

    这里// YOUR CODE 表示您不需要更改代码中已经存在的代码。

    我所做的更改主要包括:您不需要在里面设置适配器 while 循环让列表初始化所有数据,然后我们将 设置适配器外部 while loop

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多