Ajitesh Sakaray
Ajitesh Sakaray

Reputation: 39

How to display items in some order in expandable list view android?

Here I am creating a list of items using expandable list view.

Here is the code for the list items.

public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();

    List<String> business = new ArrayList<String>();
    business.add("India");

    List<String> entertainment = new ArrayList<String>();
    entertainment.add("Brazil");

    List<String> gaming = new ArrayList<String>();
    gaming.add("United States");

    List<String> general = new ArrayList<String>();
    general.add("United States");

    List<String> music = new ArrayList<String>();
   music.add("United States");

    List<String> sciandnat = new ArrayList<String>();
    sciandnat.add("United States");

    List<String> sports = new ArrayList<String>();
   sports.add("United States");

    List<String> technology = new ArrayList<String>();
   technology.add("United States");

    expandableListDetail.put("BUSINESS", business);
    expandableListDetail.put("ENTERTAINMENT",entertainment);
    expandableListDetail.put("GAMING",gaming);
    expandableListDetail.put("GENERAL", general);
    expandableListDetail.put("MUSIC", music);
    expandableListDetail.put("SCIENCE AND NATURE", sciandnat);
    expandableListDetail.put("SPORTS", sports);
   expandableListDetail.put("TECHNOLOGY", technology);
    return expandableListDetail;
}
}

I want the list to be displayed in the order as BUSINESS,ENTERTAINMENT,GAMING,GENERAL,MUSIC,SCIENCE AND NATURE,SPORTS,TECHNOLOGY

so I have given here like what I want the items to be in that order

expandableListDetail.put("BUSINESS", business);
    expandableListDetail.put("ENTERTAINMENT",entertainment);
    expandableListDetail.put("GAMING",gaming);
    expandableListDetail.put("GENERAL", general);
    expandableListDetail.put("MUSIC", music);
    expandableListDetail.put("SCIENCE AND NATURE", sciandnat);
    expandableListDetail.put("SPORTS", sports);
   expandableListDetail.put("TECHNOLOGY", technology);

but when I see the output it is like this

output image Why??

Upvotes: 0

Views: 317

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

but when i see the output it is like this, why

Because you are using a HashMap as Map fro your dataset, which doesn't preserve the inserti order of its items. Use a LinkedHashMap which preserves the insertion order. Change

 HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();

with

 Map<String, List<String>> expandableListDetail = new LinkedHashMap<String, List<String>>();

Upvotes: 1

Related Questions