可扩展列表视图中键的排序 android
Ordering of keys in expandablelistview android
我正在创建一个哈希映射并按照我希望在可扩展列表视图中显示的顺序在其中添加键。下面是将字符串放入 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 时,第二个在 first.How 之前出现,我可以按照我定义键的顺序插入 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;
}
请问可以指导一下吗?..
不是ExpandableListView
的错。HashMap
没有秩序观念。尝试使用 LinkedHashMap。来自文档
LinkedHashMap is an implementation of Map that guarantees iteration
order. All optional operations are supported.
您需要将 HashMap
替换为 LinkedHashMap
在您的代码中替换
HashMap<String, List<String>> example= new HashMap<String, List<String>>();
与
LinkedHashMap<String, List<String>> example = new LinkedHashMap<>();
我正在创建一个哈希映射并按照我希望在可扩展列表视图中显示的顺序在其中添加键。下面是将字符串放入 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 时,第二个在 first.How 之前出现,我可以按照我定义键的顺序插入 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;
}
请问可以指导一下吗?..
不是ExpandableListView
的错。HashMap
没有秩序观念。尝试使用 LinkedHashMap。来自文档
LinkedHashMap is an implementation of Map that guarantees iteration order. All optional operations are supported.
您需要将 HashMap
替换为 LinkedHashMap
在您的代码中替换
HashMap<String, List<String>> example= new HashMap<String, List<String>>();
与
LinkedHashMap<String, List<String>> example = new LinkedHashMap<>();