Skip to content Skip to sidebar Skip to footer

How To Set Data To Expandable List View?

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

Solution 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:

publicvoidListDrwaer() {

    listDataHeader = newArrayList<String>();
    listDataChild = newHashMap<String, List<String>>();

    try {
        JSONObject jsonResponse = newJSONObject(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 = newArrayList<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 = newExpandableListAdapter(this, listDataHeader, listDataChild);

    expListView.setAdapter(listAdapter);

}

Solution 2:

here example of Exapndable listview.. refer it.

try this demo

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

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

Solution 4:

You can use ArrayList like below:

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

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

Post a Comment for "How To Set Data To Expandable List View?"