如何从字符串列表中设置Spinner中的值和文本?

时间:2021-03-05 20:52:38

i am working on android spinner control. i already have populate list of string to show in spinner like

我正在研究android微调器控件。我已经填充了要在微调器中显示的字符串列表

String[] items = new String[]{ "Office", "Home", "College", "Uncle's Home", "CoDebuggers"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        SpinnerName.setAdapter(adapter);

this is working well but i need to add unique id for my list like

这工作得很好,但我需要为我的列表添加唯一ID

  1. Office = 4
  2. 办公室= 4

  3. Home = 8
  4. 主页= 8

  5. College = 9
  6. 学院= 9

  7. Uncle's Home = 10
  8. 叔叔的家= 10

  9. CoDebiggers = 55

    CoDebiggers = 55

    how can i set there value with String list? and how can i get these values by selected item change?

    如何使用String列表设置值?以及如何通过选择的项目更改获得这些值?

2 个解决方案

#1


3  

You have to create two array, one for the items and second for the item's value.

您必须创建两个数组,一个用于项目,第二个用于项目的值。

String[] items = new String[]{ "Office", "Home", "College", "Uncle's Home", "CoDebuggers"};
int[] items_value = new String[]{ 4, 8, 9, 10, 55};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
SpinnerName.setAdapter(adapter);

Get spinner selected item respective value from value array:

从值数组中获取微调器选定项的相应值:

int value = items_value[SpinnerName.getSelectedItemPosition()];

int value = items_value [Spin​​nerName.getSelectedItemPosition()];

#2


0  

Create another array, maintain sizes of both arrays same. One is for items and another one is for itemIds. inside OnclickLisener of spinner call itemsId array like below.

创建另一个数组,保持两个数组的大小相同。一个是项目,另一个是itemIds。在spinner调用itemsId数组的OnclickLisener里面,如下所示。

String[] itemsId = new String[]{ "4", "8", "9", "10","55"};
String[] items = new String[]{ "Office", "Home", "College", "Uncle's Home", "CoDebuggers"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
    SpinnerName.setAdapter(adapter);
SpinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

           String id = itemsId.get(i);
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

#1


3  

You have to create two array, one for the items and second for the item's value.

您必须创建两个数组,一个用于项目,第二个用于项目的值。

String[] items = new String[]{ "Office", "Home", "College", "Uncle's Home", "CoDebuggers"};
int[] items_value = new String[]{ 4, 8, 9, 10, 55};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
SpinnerName.setAdapter(adapter);

Get spinner selected item respective value from value array:

从值数组中获取微调器选定项的相应值:

int value = items_value[SpinnerName.getSelectedItemPosition()];

int value = items_value [Spin​​nerName.getSelectedItemPosition()];

#2


0  

Create another array, maintain sizes of both arrays same. One is for items and another one is for itemIds. inside OnclickLisener of spinner call itemsId array like below.

创建另一个数组,保持两个数组的大小相同。一个是项目,另一个是itemIds。在spinner调用itemsId数组的OnclickLisener里面,如下所示。

String[] itemsId = new String[]{ "4", "8", "9", "10","55"};
String[] items = new String[]{ "Office", "Home", "College", "Uncle's Home", "CoDebuggers"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
    SpinnerName.setAdapter(adapter);
SpinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

           String id = itemsId.get(i);
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });