如何在可扩展列表视图android中以某种顺序显示项目?

时间:2016-11-24 17:58:48

标签: android

这里我使用可扩展列表视图创建项目列表。

以下是列表项的代码。

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

我希望列表按顺序显示为商业,娱乐,游戏,一般,音乐,科学和自然,运动,科技

所以我在这里给出的就像我希望项目按顺序

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

但是当我看到输出时就像这样

output image 为什么?

1 个答案:

答案 0 :(得分:0)

  

但是当我看到输出时,就像这样,为什么

因为您使用HashMap作为数据集的地图,而不保留其项目的inserti顺序。使用保留广告订单的LinkedHashMap。变化

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

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