Search code examples
androidarraysspinnerpreference

Android Preference menu(change array id when clicked)


i got 2 classes. A class with a spinner in it and a preference class. The spinner is set up with a array from strings.xml` s1 = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
            this, R.array.height_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter1);`

The second class is the preference class with a ListPreference(think it is a a ListPreference) /kind of a spinner. If u click it and select any of the items from the ListPreference i want the spinner to use another array. Change R.array.height_array to R.array.height2_array or something like that. Possible ?


Solution

  • yes, something like this should work. If you need more than 2 choices, use more ifs

    if (prefs == choiceOne){
    ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
            this, R.array.height_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter1);
    }else{
    ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
            this, R.array.height2_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter1);
    }