【问题】
用如下代码:
@Override public RadioGroupField Get(EddlVariable var) { RadioGroupField radio = new RadioGroupField(); radio.setFieldName(var.Name); HashMap<String, String> options = new HashMap<String, String>(); for (Enumerated value : type.Enumerateds) { //Enumerated value = type.Enumerateds.get(itemIdx); Enumerated refEnumerated = MapRefEnumerated(value); if (refEnumerated == null) { options.put(value.value, value.description); } else { options.put(refEnumerated.value, refEnumerated.description); } } radio.setOptionsValue(options); return radio; } private HashMap<String, String> options = new HashMap<String, String>(); public void setOptionsValue(Object options) { this.options = (HashMap<String, String>) options; } @Override public View getInputView(Context activity) { if (this.group == null) { this.group = new RadioGroup(activity); } else { this.group.removeAllViews(); } Iterator<Entry<String, String>> iterator = this.options.entrySet() .iterator(); while (iterator.hasNext()) { RadioButton radio = new RadioButton(activity); Entry<String, String> pair = iterator.next(); radio.setTag(pair.getKey()); radio.setText(pair.getValue()); this.group.addView(radio); }
处理的内容是:
{0="Current loop", 1="Local display", 2="HART"}
结果最后,输出顺序却反了:
{2="HART", 1="Local display", 0="Current loop"}
想要实现:
对于put进去的内容的顺序,输出时,也是同样的顺序。
【折腾过程】
1.参考:
Put keys in hashmap and retrieve in same order
->
Order of values retrieved from a HashMap
去试试,
最后用如下代码:
@Override public RadioGroupField Get(EddlVariable var) { RadioGroupField radio = new RadioGroupField(); radio.setFieldName(var.Name); //HashMap<String, String> options = new HashMap<String, String>(); LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(); for (Enumerated value : type.Enumerateds) { //Enumerated value = type.Enumerateds.get(itemIdx); Enumerated refEnumerated = MapRefEnumerated(value); if (refEnumerated == null) { options.put(value.value, value.description); } else { options.put(refEnumerated.value, refEnumerated.description); } } radio.setOptionsValue(options); return radio; } //private HashMap<String, String> options = new HashMap<String, String>(); //HashMap will cause: output sequence not same with input sequence -> enumerated value sequence is reversed when show in GUI private LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(); public void setOptionsValue(Object options) { //this.options = (HashMap<String, String>) options; this.options = (LinkedHashMap<String, String>) options; } @Override public View getInputView(Context activity) { if (this.group == null) { this.group = new RadioGroup(activity); } else { this.group.removeAllViews(); } Iterator<Entry<String, String>> iterator = this.options.entrySet() .iterator(); while (iterator.hasNext()) { RadioButton radio = new RadioButton(activity); Entry<String, String> pair = iterator.next(); radio.setTag(pair.getKey()); radio.setText(pair.getValue()); this.group.addView(radio); }
最终实现了要的效果:
输入的顺序:
{ 0, "Current loop" }, { 1, "Local display" }, { 2, "HART" }
和输出的效果,是一致的:
【总结】
HashMap无法保证,输出顺序和输入顺序一致;
改为LinkedHashMap,即可保证,输出顺序和输入顺序一致。
转载请注明:在路上 » 【已解决】HashMap输出的项的顺序不固定