【问题标题】:How to set data to Expandable List View?如何将数据设置为可扩展列表视图?
【发布时间】:2014-01-04 05:40:43
【问题描述】:

我尝试为我的应用程序添加可扩展列表视图。该列表包括标题和子列表。

对于列表,使用 Web 服务加载的数据。

我使用Android Expandable List View Tutorial 链接进行开发。

我已成功加载标题列表。但情况是,子列表包括数组列表中的所有数据。我只想加载与标题相关的内容。

例如:它显示酒店名称和所有酒店详细信息,再次显示酒店名称和所有酒店详细信息。

它通过网络服务成功加载数据。我只想处理arrayList。

这是我使用的代码。

    public void ListDrwaer() {

        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
        List<String> restData = new ArrayList<String>();

        try {
            JSONObject jsonResponse = new JSONObject(strJson1);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");

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

                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String restName = jsonChildNode.optString("name");
                String address = jsonChildNode.optString("address");
                String mobile = jsonChildNode.optString("mobile");
                String direction = jsonChildNode.optString("direction");
                String bestTime = jsonChildNode.optString("bestTime");
                String food = jsonChildNode.optString("food");
                String dress = jsonChildNode.optString("dress");
                String priceRange = jsonChildNode.optString("priceRange");

                listDataHeader.add(restName);

                restData.add(address);
                restData.add(mobile);
                restData.add(direction);
                restData.add(bestTime);
                restData.add(food);
                restData.add(dress);
                restData.add(priceRange);

                listDataChild.put(listDataHeader.get(i), restData);
            }

        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        expListView.setAdapter(listAdapter);

    }

谁能帮帮我?

【问题讨论】:

    标签: android android-layout arraylist expandablelistview expandablelistadapter


    【解决方案1】:

    您需要为 for 循环中的每个项目创建一个新的 restData,否则它将为每个项目保留对相同的引用:

    public void ListDrwaer() {
    
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
    
        try {
            JSONObject jsonResponse = new JSONObject(strJson1);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");
    
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String restName = jsonChildNode.optString("name");
                String address = jsonChildNode.optString("address");
                String mobile = jsonChildNode.optString("mobile");
                String direction = jsonChildNode.optString("direction");
                String bestTime = jsonChildNode.optString("bestTime");
                String food = jsonChildNode.optString("food");
                String dress = jsonChildNode.optString("dress");
                String priceRange = jsonChildNode.optString("priceRange");
    
                List<String> restData = new ArrayList<String>();
                restData.add(address);
                restData.add(mobile);
                restData.add(direction);
                restData.add(bestTime);
                restData.add(food);
                restData.add(dress);
                restData.add(priceRange);
    
                listDataHeader.add(restName);
                listDataChild.put(restName, restData);
            }
    
        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
                    Toast.LENGTH_LONG).show();
        }
    
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
    
        expListView.setAdapter(listAdapter);
    
    }
    

    【讨论】:

      【解决方案2】:

      这里 example 的 Exapndable 列表视图.. 参考它。

      试试这个demo

      在那里你可以看到如何在另一个 binClass 中设置 ArrayList。

      【讨论】:

      【解决方案3】:

      试试这个:

          // Adding child data
          listDataHeader.add("Top 250");
          listDataHeader.add("Now Showing");
          listDataHeader.add("Coming Soon..");
      
          // Adding child data
          List<String> top250 = new ArrayList<String>();
          top250.add("The Shawshank Redemption");
          top250.add("The Godfather");
          top250.add("The Godfather: Part II");
          top250.add("Pulp Fiction");
          top250.add("The Good, the Bad and the Ugly");
          top250.add("The Dark Knight");
          top250.add("12 Angry Men");
      
          List<String> nowShowing = new ArrayList<String>();
          nowShowing.add("The Conjuring");
          nowShowing.add("Despicable Me 2");
          nowShowing.add("Turbo");
          nowShowing.add("Grown Ups 2");
          nowShowing.add("Red 2");
          nowShowing.add("The Wolverine");
      
          List<String> comingSoon = new ArrayList<String>();
          comingSoon.add("2 Guns");
          comingSoon.add("The Smurfs 2");
          comingSoon.add("The Spectacular Now");
          comingSoon.add("The Canyons");
          comingSoon.add("Europa Report");
      
          listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
          listDataChild.put(listDataHeader.get(1), nowShowing);
          listDataChild.put(listDataHeader.get(2), comingSoon);
      

      【讨论】:

      • 不,亲爱的。我从 API、JSON 加载该数据。所以我不能像这样添加。
      【解决方案4】:

      你可以像下面这样使用 ArrayList:

      ArrayList<String> groupItem = new ArrayList<String>();
      ArrayList<Object> childItem = new ArrayList<Object>();
      

      使用 JSON 解析。这对你有用。我只能给你逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        相关资源
        最近更新 更多