简体   繁体   English

如何使用 HashMap 并在 ArrayAdapter 中使用它以在微调器中显示值

[英]How to use a HashMap and use it in an ArrayAdapter to display the values in a spinner

I have a HashMap which contains a key and value, both of which are strings.我有一个包含键和值的 HashMap,它们都是字符串。 I am getting the HashMap from a different activity.我从不同的活动中获取 HashMap。 I am trying to use the HashMap and put it into an array adapter so I can display a spinner with the values of the HashMap.我正在尝试使用 HashMap 并将其放入数组适配器中,以便我可以显示带有 HashMap 值的微调器。 I have tried to iterate through the HashMap and haven't had any luck.我试图遍历 HashMap 并且没有任何运气。

HashMap<String, String> examCodesMap = (HashMap<String, String>) MainActivity.examCodesMap;
HashMap<String, String> examCodesMap = new HashMap<String, String>();
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList<String> arrayList = new ArrayList<>();

for(Map.Entry<String, HashMap> entry : selects.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();

    arrayList.add(value);
}

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.spinner, arrayList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM