final Spinner spinner = (Spinner)findViewById(R.id.spinnerNumber);
List<Integer> list = new ArrayList<>();
for (int j = 0; j < 51; j++) {
list.add(j);
}
ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
我想在此微调器中设置一个选定的项目。通常我会在spinner.setSelection()
内setSelection()
插入我想要设置的元素的位置。这次我放在setSelection()
内的所有东西都没有产生任何效果,我的微调器仍然设置为0 ......我错过了什么?
答案 0 :(得分:1)
尝试这个
Spinner spinner = (Spinner)findViewById(R.id.spinnerNumber);
final List<Integer> list = new ArrayList<Integer>();
for (int j = 0; j < 51; j++) {
list.add(j);
}
ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Selected item :: "+list.get(position));
System.out.println("Selected item Pos :: "+position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
这将有效
int spinnerPosition = adapter.getPosition(yourStringValue);
mSpinner.setSelection(spinnerPosition);