How to set data to Expandable List View?

2019-08-30 21:41发布

问题:

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

for the list, data loaded by using web service.

i used Android Expandable List View Tutorial link for develop this.

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.

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

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

    }

Can anyone help me?

回答1:

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

}


回答2:

here example of Exapndable listview.. refer it.

try this demo

at there you can see how to set ArrayList in another binClass.



回答3:

Try This:

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


回答4:

You can use ArrayList like below:

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

use JSON parsing. This will works for you. I can give you logic only.