簡體   English   中英

Android將微分值設置為微調器中的選定項目,並彈出微調器中顯示的項目

[英]Android set diffrent value to selected item in spinner with items shown in the spinner pop up

我在hashMap中列出了國家/地區及其電話號碼,如下所示:

hashmap.put("Angola", "+244");

我想將國家名稱的數組設置為微調器適配器,並且當用戶單擊微調器以選擇一個國家時,向她/他顯示國家名稱列表,但是當他/她在微調器文本中選擇一個國家時,將顯示國家/地區代碼。 我不知道該怎么做。 這些圖像可能有助於理解我的意思: 在彈出的微調框選擇中 微調器中顯示

這可能與您訪問名稱的方式非常相似。 不幸的是,地圖沒有按位置索引,因此您必須遍歷條目,鍵或值。 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM