I have a spinner like this:
// Spinner 1
final Spinner plan = (Spinner) dialog.findViewById(R.id.spinner1);
strings = getResources().getStringArray(R.array.paymentplan);
sAdapter = new SpinnerAdapter(this,
android.R.layout.simple_spinner_item, strings);
sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plan.setAdapter(sAdapter);
// plan.setAdapter(spinner1Adapter);
plan.setSelection(prefsDisplay.getInt("spinnerSelection1", 0));
plan.setOnItemSelectedListener(new MyOnItemSelectedListenerPlan());
When user clicks, I want it to save state:
public void onClick(View v) {
Editor editor2 = prefsPlan.edit();
int selectedPosition1 = plan.getSelectedItemPosition();
editor2.putInt("spinnerSelection1", selectedPosition1);
editor2.commit();
}
It saves the position in SharedPref, but the spinner goes back to default. Anyone see something here?
you are storing spinnerSelection
editor1.putInt("spinnerSelection", selectedPosition);
an accessing spinnerSelection1
prefsDisplay.getInt("spinnerSelection1", 0)
make them consistent.
Update
when you are accessing plan.getSelectedItemPosition(). then spinner is visible? I guess NO.
try to put a public variable for selected position. And update selected position in your MyOnItemSelectedListenerPlan. And then store that position in shared preferences. I guess it solve your problem.
to Save:
int selectedPosition = yourSpinner.getSelectedItemPosition()
editor.putInt("spinnerSelection", selectedPosition);
editor.commit();
to Load:
yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));
if you are array used it should changed like this
String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
editor.putString("spinnerSelection", selectedString);
editor.commit();
checking array[i] against the value stored in prefs.if you use an
ArrayList instead this part could be done without the loop by calling
ArrayList.indexOf(prefs.getString("spinnerSelection", "");
when you commit show all above array item gone. show no one into
array.
Try below code and first save position of current selected item into one integer variable on onItemSelectedListener() using below code and after that store this variable value into shared preferences.
For Store value into one Variable.
int index;
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// Here Position is Item Index
index = position;
}
For Store Value into shared preferences.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt("SelectedIndex", index);
prefsEditor.commit();
And see below link for more information
Android Spinners
Android Shared Preferences
after saving to the preferences, you have to set the selected item to the spinner for further uses
as
int pos = prefsDisplay.getInt("spinnerSelection", "0");
display.setSelection(pos);
but you are using spinnerSelection1. so By default if there is no matching in the preferences. the default value will return. so Here 0 is returned and spinner is set to first position
Related
What I have is a Settings activity on which I choose the language via spinner. When I change it to Russian for example the language changes, but when I open the Settings menu again the selected item on the spinner is the first item (English) and not the current one (Russian).
This is my spinner
Resources res = getResources();
language = res.getStringArray(R.array.languages_arrays);
Spinner spinner = (Spinner) findViewById(R.id.toolbar_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, R.layout.spinner_item_dropdown,
language);
spinner.setAdapter(adapter);
What do I need to change in the spinner in order to show the selected language?
If you want to set the previous selected item of a spinner you have to store the selected item and then set your selection :
here's an example wish i'am getting my previous selected item then fetching the item's position from he's array adapter
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String language = preferences.getString("language", "");
if(!language.equalsIgnoreCase(""))
{
int spinnerPosition = arrayAdapter.getPosition(language);
spinner.setSelection(spinnerPosition);
}
You can store your data as below :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
SharedPreferences.Editor editor = preferences.edit();
editor.putString("language",spinner.getSelectedItem().toString(););
editor.apply();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
For that u need to set spinner value programatically
for (int i=0 ;i<languages.length;i++)
{
if(languages[i] = "RUSSIAN")//change it according to ur prrefence
spinner.setSelection(i);
}
You have to save previous selected value somewhere in your app. When you open setting again check for that value and find position of it in array. Once you find location of it set Spinner selection to that position like below:
spinner.setSelection(position_of_selected_item);
I have to keep Spinner selected item remain same, after onRestart() or onResume() the application. Spinner items are getting populated from sqlite database using ArrayAdapter. so guide me how do I achieve this.
String myString = (String) myspinner.getSelectedItem();` //the value you want the position for
` #SuppressWarnings("unchecked")
ArrayAdapter<String> myAdap = (ArrayAdapter<String>) myspinner.getAdapter(); //cast to an ArrayAdapter
int spinnerPosition = myAdap.getPosition(myString);
//set the default according to value
myspinner.setSelection(spinnerPosition);
You have to store last selected position into share-Preference or in database and when app restart then pass that appropriate stored position to your spinner, and keep updating SP while changing position into spinner.
Store position:
SharedPreferences SP;
SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SP.edit().putInt("last index", spin.getSelectedItemPosition()).commit();
Retrieve and set to spinner:
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(SP!=null){
int pos = SP.getInt("last index", 0);
spin.setSelection(pos);
}
you can save the position of spinner in onSaveInstanceState() and retrive the spinner position
from onRestoreInstanceState (). you can use a common global variable for update the spinner
position.
and use this variable in onResume() method .
hope that it will work
check it
Hello I have a profile layout file wherein it saves the data the user inputs. This layout has checkboxes, textviews, and spinners. Then it will be viewed in my userprofile layout file as in textviews. I'm saving them to sharedpreferences. What I'd like to do is retrieve the String value the user selected/saved from spinners, display it in a textview. How can I do this?
I'm wondering how to implement this:
tvbloodtype.setText(prefs.getInt("bloodtype",0));
Any help is truly appreciated. Thanks.
I got error:
ResourcesNotFoundException: String resource ID
After you have your spinner object you can call yourSpinner.OnItemSelectedListener() to get the object that was selected. Then you can save your item in your TextView
yourSpinner.OnItemSelectedListener(new OnItemSelectedListener {
public void onItemSelected(AdapterView <? > parentview, View v, int position, long id) {
curPos = position;
String selected = parentView.getItemAtPosition(position).toString();
tvbloodtype.setText(selected);
}
});
Also reference: How do you get the selected value of a Spinner?
you will need to use String.valueOf() or Integer.toString() for showing Integer in TextView . Example :
tvbloodtype.setText(String.valueOf(prefs.getInt("bloodtype",0)));
OR
tvbloodtype.setText(Integer.toString(prefs.getInt("bloodtype",0)));
write preferences like
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key, value); //key="YOUR_KEY_STRING" //value = your boolean value
editor.apply();
read from preferences
Boolean yourBoolean = prefs.getBoolean("YOUR_KEY_STRING",false); //false = value, if value wasn t set
whenever you are saving your value to sharedPreferences.
SharedPreferences prefs= getSharedPreferences("prf", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("bloodtype", spinner.getSelectedItem().toString());
edit.commit();
when you are retriving
tvbloodtype.setText(spinner.getString("bloodtype", ""));
How would you save and retrieve a spinners selection, so when you come back the same item on the spinner is selected? Maybe with shared preferences?
to Save data on the sharedPreferences( put this code on the onItemSelected() method and save the selected value's position of your spinner) :
int userChoice = spinner.getSelectedItemPosition();
SharedPreferences sharedPref = getSharedPreferences("FileName",0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("userChoiceSpinner",usersChoice);
prefEditor.commit();
To get data from sharedPreferences :
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
int spinnerValue = sharedPref.getInt("userChoiceSpinner",-1);
if(spinnerValue != -1) {
// set the selected value of the spinner
spinner.setSelection(spinnerValue);
}
refer this : set selection in spinner
and this : get the position of the selected item in a spinner
See Also :
Android Tutorial : Using SharedPreferences
Android Tutorial : Switch Between Activities and Pass Data Between Them
I already know how to save edittext contents to sharedpreferences but in spinners and radiogroups I still don't have a clue. Can you please give me snippets of codes how to do it? Thanks
For the data store is is not relevant which UI elements is used to display or modify the value. Here is a description how to store or retrieve various data types: http://developer.android.com/reference/android/content/SharedPreferences.html
So a spinner selection is simply an integer (or string if you like) and the choice for a radio group is simply whatever identifier (as string) you choose to represent that choice.
If the choices come from an array resource you may use the values from the array or the index into the array. Store/retrieve them in/from the shared preferences like you used to store and retrieve the text from the EditText.
This is the way that you can save the selected item of a spinner in sharedPreferences:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object obj = parent.getItemAtPosition(pos);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = prefs.edit();
prefsEditor.putString("object", obj.toString());
prefsEditor.commit();
}
public void onNothingSelected(AdapterView<?> parent) { }
});