I am in my Fragment class which has spinner that contains locales..for text to speech
mylocale = localeList.get(position);
textToSpeech.setLanguage(mylocale);
spinnerposition = Integer.valueOf(String.valueOf(localeList.get(position)));
i want to save the position using sharedpreferences
this is what i am trying:
public void savepos(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(POSITION , spinnerposition);
editor.apply();
}
i have declared spinner position as Global variable
int spinnerposition;
here's how i retrieve the position:
public void saveposload(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
int position = sharedPreferences.getInt(POSITION ,1);
mspinner.setSelection(sharedPreferences.getInt(POSITION, position));
}
but i am not able to do so, app crashes please help
heres the exception from log
2018-12-30 15:13:49.904 3611-3611/my.uapp.com E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.uapp.com, PID: 3611
java.lang.NumberFormatException: For input string: "it_IT"
at java.lang.Integer.parseInt(Integer.java:521)
at java.lang.Integer.valueOf(Integer.java:611)
at my.uapp.com.Tab0$override.onItemSelected(Tab0.java:363)
at my.uapp.com.Tab0$override.access$dispatch(Tab0.java)
at my.uapp.com.Tab0.onItemSelected(Tab0.java)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Is localeList
the list where you store all the locales, like "it_IT"
, etc?
If this is the case then this:
localeList.get(position)
is a String and it cannot be parsed to int with this line:
spinnerposition = Integer.valueOf(String.valueOf(localeList.get(position)));
Maybe you only need this:
public void saveposload(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
mspinner.setSelection(sharedPreferences.getInt(POSITION, 0));
}
Make sure that you call saveposload()
after you set the adapter.