L
L
Liankir2013-12-04 23:22:26
Android
Liankir, 2013-12-04 23:22:26

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) {

        }
    });
}
}

This works fine as long as the application has only one language, but when one (multiple) localization is added, the string resources will be translated into the appropriate language, and the elements in <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;
        }
    }
});

I do not like this approach, and I wanted to know if there is another, more correct and elegant implementation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bimeg, 2013-12-05
@bimeg

<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>

In other languages ​​change the order accordingly.
And processing:
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 question

Ask a Question

731 491 924 answers to any question