I have button, after I click button it will list to the ListView, choose ListView item and this item will display to left of the button, I want after I restart the program the listItem was choosed is display.
If anybody know this, please help me? Thanks
There are 2 possible simple ways:
SharedPreference (simplest)
SQLite (maybe for your purpose is useless)
If you have to save only text chosen from a spinner, well, SharedPreference could be the solution. This is how to use it:
READING VALUE from your PREFERENCES_LIST
// set name of your preferences list
private static String MY_PREFERENCES = "my_preferences_list";
// set key for retrieving text you saved
private static String TEXT_DATA_KEY = "last_text_spinner_choise";
// create a pointer to your preferences list specifying name and access typology
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
// get String from preferences list specifying KEY of Text you want retrieve and a default string if key doesn't exist
String textData = prefs.getString(TEXT_DATA_KEY, "No Preferences!");
// your textView or something else
TextView outputView = (TextView) findViewById(R.id.outputData);
// set textView with value taken from your preference list
outputView.setText(textData);
WRITING VALUE in your PREFERENCES_LIST
// Same as before
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
// Editor pointer for changing value in your preference list
SharedPreferences.Editor editor = prefs.edit();
// your editText or spinner from which you want take text value
EditText outputView = (EditText) findViewById(R.id.inputData);
CharSequence textData = outputView.getText();
if (textData != null) {
// Here you save textData in your preferences_list. You have to specify key and value. Key is important for retrieving text as seen before
editor.putString(TEXT_DATA_KEY, textData.toString());
// Confirm your choice by commit()
editor.commit();
}
Hope it helps
Related
I have a textview that when user clicks it, I want it to be invisible. But I want it to stay invisible, even after user has reloaded the app, using "sharedpreferences". How to get shared preferences working and then recalled correctly?
This is my code for storing the preference when user clicks the textview:
textView.setVisibility(View.INVISIBLE);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("was_clicked", true);
editor.commit(); // commit changes
So I understand this will store a true/false boolean in shared preferences called" was_clicked".
But now how do I have it checked for "true" in the onCreate method of the activity and then have it set TextView to view.INVISIBLE if was_clicked = true?
Check out this article. But to answer your question specifically
if (getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE).getBoolean("was_clicked", false))
textView.setVisibility(View.INVISIBLE);
You can check the value using this method:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
boolean wasClicked = pref.getBoolean("was_clicked",false);
if (wasClicked){
textView.setVisibility(View.INVISIBLE);
}else {
textView.setVisibility(View.VISIBLE);
}
In your onCreate method you should create preferences and read from them like this:
static final String PREFERENCES_NAME = "MyPref";
static final String WAS_CLICKED_NAME = "was_clicked";
#Override
void onCreate()
{
SharedPreferences preferences = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
boolean invisible = preferences.getBoolean(WAS_CLICKED_NAME, false); // 2nd argument is the default value
if (invisible)
{
textView.setVisibility(View.INVISIBLE);
}
}
getBoolean method will read value of "was_clicked" preference. 2nd argument will be returned if you haven't yet put value "was_clicked" to shared preferences, so you can return false if you haven't put this preference to show the textView.
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 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));
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'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();
}