Save Spinner with SharedPreferences in fragment (O

2019-06-09 20:47发布

问题:

I am new to Android programming and at the moment I am trying to make some spinners with four array elements (string-array), and then save the current state with shared preferences. I've already done a lot of research, but most of the solutions only work for activities, and I am working in a fragment.

The code now is as follows:

FragmentSection3.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v = inflater.inflate(R.layout.preferences, container, false);

final Spinner spinner2 = (Spinner) v.findViewById(R.id.pref2_spinner);
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
                getActivity(), R.array.spinner_array, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);

    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());  //context
            SharedPreferences.Editor prefEditor = prefs.edit();
            prefEditor.putString("savedValue",spinner2.getSelectedItem().toString());
            prefEditor.commit();

            String savedValue=PreferenceManager
                    .getDefaultSharedPreferences(getActivity().getBaseContext()) //context
                    .getString("savedValue","");

            for(int i=0; i<4; i++)
                if(savedValue.equals(spinner2.getItemAtPosition(i).toString())){
                    spinner2.setSelection(i);
                    break;
                }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent){}
    });

    return v;
 }

My Spinner in xml

preferences.xml

<Spinner
            android:layout_weight="50"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pref2_spinner" />

String Array

strings.xml

    <string-array name="spinner_array">
        <item>Keine</item>
        <item>Eher Keine</item>
        <item>Große</item>
        <item>Eher Große</item>
    </string-array>

I don't know why I can't save my spinner with sharedpreferences, every time I leave the fragment it sets everything back.

回答1:

You are saving the value at the right place but restoring it at the wrong place. What is the purpose of doing

String savedValue=PreferenceManager
.getDefaultSharedPreferences(getActivity().getBaseContext()) //context
.getString("savedValue",""); 

for(int i=0; i<4; i++)
    if(savedValue.equals(spinner2.getItemAtPosition(i).toString())){
        spinner2.setSelection(i);
        break;
}

inside the OnItemSelectedListener() ?

You should restore the value from the sharedPreferences when the view is created and save it when an item is selected. Move the code above outside of the listener and it should be working