[英]Android set diffrent value to selected item in spinner with items shown in the spinner pop up
這可能與您訪問名稱的方式非常相似。 不幸的是,地圖沒有按位置索引,因此您必須遍歷條目,鍵或值。 LinkedHashMap
在這里可能很有用。 這是一個簡單的測試用例:
@RunWith(JUnit4.class)
public class Test {
// NOTE: Using `LinkedHashMap` here to ensure that
// the entries will be ordered by insertion.
Map<String, String> map = new LinkedHashMap<>();
@Before
public void setUp() throws Exception {
map.put("Country1", "+123");
map.put("Country2", "+456");
}
@Test
public void test() throws Exception {
assertEquals("+123", getValueForPosition(0));
assertEquals("+456", getValueForPosition(1));
}
private String getValueForPosition(int position) {
int i = 0;
for (String s : map.values()) {
if (i == position) {
return s;
}
i += 1;
}
return null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.