2

I have a fragement with various Spinners in it. To have these spinners display an initial (non selectable) value I'm using a custom arrayAdapter (SpinnerHintAdapter). The only important thing this class does is override the getCount() so the last item of the selection-array isn't displayed, this is where the initial value is stored.
This all works fine untill you rotate the device, then the spinners are set on the last normal value of the list for some reason, even though the Fragment class still thinks it's set on the initial value.
Anyone any clue why this happens and / or how to solve this problem?

Code samples:
fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_free_top_up, container, false);
    Spinner pet = (Spinner) rootView.findViewById(R.id.pet);
    SpinnerHintAdapter<CharSequence> petAdapter = SpinnerHintAdapter.createFromResource(getActivity(),
        R.array.pet_array, android.R.layout.simple_spinner_item);
    petAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    pet.setAdapter(petAdapter);
    pet.setSelection(pet.getCount());
    return rootView;
}

SpinnerHintAdapter:

@Override
public int getCount() {
    int count = super.getCount();
    // The last item will be the hint.
    return count > 0 ? count - 1 : count;
}

example string-array

<string-array name="pet_array">
    <item>Yes</item>
    <item>No</item>
    <item>(initial value)</item>
</string-array>
dashhund
  • 322
  • 3
  • 17
  • It seems `pet.setSelection(pet.getCount())` should be called with `0` or `1` caz `pet.getCount()` returns `2`. Or your hacky `getCount` confuse somethin in Spinner. :/ – ytRino Feb 24 '15 at 10:27
  • It's supposed to return 2 because I want the initial value to be displayed – dashhund Feb 24 '15 at 10:42
  • Perhaps you can avoid all this trouble by using the spinner only for what it's intended (have selectable items) and display this other item in another fashion? – lordstyx Feb 24 '15 at 13:00
  • I want to solve the problem because this fits what i'm creating, i'm not looking for a way to avoid it. – dashhund Feb 24 '15 at 13:12

2 Answers2

1

The activity is re-created when you rotate the device. You need to override onSavedInstanceState function, save your data in a Bundle and then use that data in onCreate again.

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //save your data
}

and then use that in your onCreate to restore your spinner.

yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • That's not what i asked, i know this but it's irrelevant to my question. The problem is that the inital value, which is supposed to be set in the onCreate method isn't getting selected anymore, another value is. At this point, every time i rotate the device the value should reset to the initial value (last of the list) but instead it sets to the last but one element in the list. – dashhund Feb 24 '15 at 09:21
  • i think this answer is much helpful. It could be happen that Activity is re-created **and** fragment is **not** re-created. – ytRino Feb 24 '15 at 10:33
  • no, I checked that, fragment's onCreate method is called again – dashhund Feb 24 '15 at 10:39
  • You wrote `onCreateView` not `onCreate` in question. both is called? – ytRino Feb 24 '15 at 10:46
  • sorry, i meant `onCreateView`, I assume `onCreate` is called too but i'm not using that at the moment. – dashhund Feb 24 '15 at 10:54
0

Haven't found an answer to this specific problem but used another method to display an initial value which can be found here: https://stackoverflow.com/a/12221309/3453217

Any clarification as to what went wrong with my first attempt is still welcome.

Community
  • 1
  • 1
dashhund
  • 322
  • 3
  • 17