save value of spinner selected item - android

How can I save the current selected spinner value, such that when I reopen the application the saved value is automatically selected by default?

Please Write below code on onItemSelectedListener() of spinner and store selected value into shared preferences.
String mSpnValue=mSpinner1.getSelectedItem().toString();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Value", mSpnValue);
Use below code for set item as selected in spinner.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String myString = myPrefs.getString("Value","nothing"); // the value you want the
ArrayAdapter<String> myAdap = (ArrayAdapter<String>) mSpinner1.getAdapter();
int spinnerPosition = myAdap.getPosition(myString);
// set the default according to value
mSpinner1.setSelection(spinnerPosition);

You can save spinner position in preferences and when entering back use spinner.setSelection(position_from_preferences);

You can use several methods
For example you can use a DataBase and save on it.
Other methods, and the best IMO, is used SharedPreferences
http://developer.android.com/intl/es/reference/android/content/SharedPreferences.html
http://developer.android.com/intl/es/reference/android/app/backup/SharedPreferencesBackupHelper.html

For set value one another way:---
for(int i=0;i<adapter.getCount();i++){
if(adapter.getItem(i).equals(your save preference value){
spinner_timer.setSelection(i);
}
}

you can also refer to the spinner value by position. that way you need only to deal with ints straight-fowardly:
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("VALUE", spinner.getSelectedItemPosition());
editor.commit();
and to load:
spinner.setSelection(settings.getInt("VALUE", 0));

Related

android custom list adapter to remember databse id

How do I create a list adapter that remember the id as well as the content?
I want to make it in a way that when I initialize the adapter, I will feed it a map with id as the key and the content as the value, as opposed to just a list of strings.
What do I have to subclass, and what I put for the constructor?
You can try that with sharePreferneces. It tried it works fine for me.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt(key,value);
prefsEditor.commit();

How to retrieve spinner value from sharedpreferences? ANDROID

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", ""));

Shared Preference & Spinner Not Maintaining State

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

Save and Retrieve Selected Spinner Position

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

Spinner value into a ListPreference

I'm trying to save a Spinner value into a ListPreference. I can't get it to work. I've tried to get this working for a long time now. Does anyone have a solution or can anyone point me in the right direction.
So this is what I have:
SharedPreferences preferences;
private static final String KEY_WEIGHT_PREFERENCE = "weightunit";
...
preferences = PreferenceManager.getDefaultSharedPreferences(this);
...
This is the main part, both the Spinner and the ListPreference grab the same data from an array xml.
SharedPreferences.Editor edit = preferences.edit();
Spinner weight = (Spinner) findViewById(R.id.weightUnitSpinner);
int selectedPosition = weight.getSelectedItemPosition();
edit.putInt(KEY_WEIGHT_PREFERENCE, selectedPosition);
edit.commit();
Thanks!
What isn't working?
There's a sample app called Spinner that contains a sample Spinner. It saves the state of the Spinner to saved preferences in onPause(), and restores it in onResume().
I found the answer, the SpinnerValue needs to be saved as a string in order to get recognized by the ListPreference.
Here's my final code:
private void updatePreferenceWeightValue() {
SharedPreferences.Editor edit = preferences.edit();
Spinner weight = (Spinner) findViewById(R.id.weightUnitSpinner);
int selectedPosition = weight.getSelectedItemPosition();
String weightValue = "";
weightValue = Integer.toString(selectedPosition);
edit.putString(KEY_WEIGHT_PREFERENCE, weightValue);
edit.commit();
}

Categories

Resources