Answer the question
In order to leave comments, you need to log in
Correct handling of the selection of Spinner'a elements, subject to the presence of several localizations?
There is a dropdown list (Spinner) filled with string elements that are contained in <string-array>
and sorted alphabetically. When I need to handle the selection of an element by the user, I use something like this:
public class MyActivity extends Activity {
Spinner ct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ct = (Spinner) findViewById(R.id.spinner);
ArrayAdapter categories = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.some_array));
categories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ct.setAdapter(categories);
ct.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
//performing action #1
break;
case 1:
//performing action #2
break;
etc.
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
<string-array>
will have a slightly different order. Accordingly, the above code will not be able to correctly process the user's choice, since the order of the items in the dropdown list will be different for each localization. In this case, I have to use the following option:ct.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String chosenStr = ct.getSelectedItem().toString();
switch (chosenStr) {
case "Строка #1 в первой локализации":
//performing some action #1
break;
case "Строка #1 во второй локализации":
//performing some action #1
break;
case "Строка #2 в первой локализации":
//performing some action #2
break;
case "Строка #2 во второй локализации":
//performing some action #2
break;
}
}
});
Answer the question
In order to leave comments, you need to log in
<string-array>
Can be kept
next to<integer-array>
<string-array name="arr">
<item >Str1</item>
<item >Str2</item>
<item >Str3</item>
</string-array>
<integer-array name="arrInd">
<item >1</item>
<item >2</item>
<item >3</item>
</integer-array>
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
int globalInd = getResources().getIntArray(R.array.arrInd)[position];
switch (globalInd)
{
...
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question