在expandablelistview android中排序键

时间:2015-10-18 14:13:53

标签: android expandablelistview

我正在创建一个hashmap,并按照我想在expandable listview中显示的顺序在其中添加键。下面是将字符串放入hashmap的代码:

HashMap<String, List<String>> example= new HashMap<String, List<String>>();
        example.put(cnt.getString(R.string.first), first_summary);
        example.put(cnt.getString(R.string.second), second_summary);
        example.put(cnt.getString(R.string.third), third_summary);

但是当我将它添加到listview时,第二个在第一个之前出现。我如何按照我定义键的顺序插入expandablelistview?

以下是expandablelistview创建的代码:

 public View getGroupView(int parent, boolean isExpanded, View convertview, ViewGroup parentview) {
    // TODO Auto-generated method stub
    String group_title = (String) getGroup(parent);
    if(convertview == null)
    {
        LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertview = inflator.inflate(R.layout.parent_layout, parentview,false);
    }
    TextView parent_textview = (TextView) convertview.findViewById(R.id.parent_txt);
    parent_textview.setTypeface(null, Typeface.BOLD);
    parent_textview.setText(group_title);
    return convertview;
}

你能指导吗?..

2 个答案:

答案 0 :(得分:4)

It's not $scope.selectValue="2"; $scope.availableOptions = [ {id: '1', name: 'Option A'}, {id: '2', name: 'Option B'}, {id: '3', name: 'Option C'} ]; 's fault .ExpandableListView has not the notion of order. Try using a LinkedHashMap. From the doc

LinkedHashMap is an implementation of Map that guarantees iteration order. All optional operations are supported.

答案 1 :(得分:0)

您需要将HashMap替换为LinkedHashMap

在您的代码中替换

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

使用

LinkedHashMap<String, List<String>> example = new LinkedHashMap<>();