Set Key and Value in spinner

前端 未结 3 1696
孤街浪徒
孤街浪徒 2020-11-27 12:13

I have a spiner and I want set a key and a value on this,I use of HashMap,It\'s work,but show one row,like this:

3条回答
  •  再見小時候
    2020-11-27 13:07

    Try to use HashMap to store Key-Value pair data and in your case spinner item index as key and Province_ID as value. Check below example for more details.

    Prepare value for spinner

    String[] spinnerArray = new String[Province_ID.size()];
    HashMap spinnerMap = new HashMap();
    for (int i = 0; i < Province_ID.size(); i++)
    {
       spinnerMap.put(i,Province_ID.get(i));
       spinnerArray[i] = Province_NAME.get(i);
    }
    

    Set value to spinner

    ArrayAdapter adapter =new ArrayAdapter(context,android.R.layout.simple_spinner_item, spinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    Get value to spinner

    String name = spinner.getSelectedItem().toString();
    String id = spinnerMap.get(spinner.getSelectedItemPosition());
    

提交回复
热议问题