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
- Office = 4
- Home = 8
- College = 9
- Uncle's Home = 10
-
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列表设置值?以及如何通过选择的项目更改获得这些值?
办公室= 4
主页= 8
学院= 9
叔叔的家= 10
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 [SpinnerName.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 [SpinnerName.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) {
}
});