如何数据集可扩展列表视图?

如何数据集可扩展列表视图?

问题描述:

我尝试添加扩展列表视图我的申请。这份名单包括报头和子列表中。

I tried to add Expandable List View for my application. That list include header and sub list.

为列表,数据通过使用Web服务加载。

for the list, data loaded by using web service.

我用 Android的可扩展列表视图教程链接开发这个。

我装头列表成功。但情况是,子列表包含数组列表的所有数据。我想只加载它与头相关的东西。

i loaded header list successfully. but the case is, the sublist include all data in array list. i want to load only the things which related with the header.

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

for an example: it display hotel name and all hotels details, again hotel name and all hotels details.

它成功地通过Web服务加载数据。我只是想处理的ArrayList

It successfully load the data through the web service. i just want to handle the arrayList.

这是$ C C I使用。

This is code i used.

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

    }

谁能帮我?

您需要创建一个新的 restData ​​ code>为您的for循环的每个项目,否则会保持参照同一个用于每个文件:

You need to create a new restData for each item in your for loop otherwise it'll keep the reference to the same one for each item:

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

}