Android Preference old and new value compare - android

I have got NewValue from activity1 and store on preference and compare with OldValue from activity2: my problem it dose not store oldValue..
on activity 1:
int i = 5;
SharedPreferences prefs1 = getPreferences(0);
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putInt("new", i);
editor.commit();
on activity 2:
SharedPreferences prefs1 = getPreferences(0);
int oldValue = prefs1.getInt("old", 0);
int newValue = prefs1.getInt("new", 0);
/* Should Activity Check for Updates Now? */
if (oldValue < newValue) {
/* Save current newValue for next Check */
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putInt("old", newValue);
editor.commit();
do something....
}

Well, if you want share preferences with activities then you need to use getSharedPreferences(String, int) but you are using getSharedPreferences(int)
getSharedPreferences(int) Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.
get SharedPreference object like this
SharedPreferences prefs1 =getSharedPreferences ("app_prefs", Context.MODE_PRIVATE);

is the key of newValue is "new" or "newValue" ? you saved it with key and retrieving with the other
So it won't find the newValue so it will be always equals zero
editor.putInt("newValue", 5);
int newValue = prefs1.getInt("new", 0);
So you newValue will equal 0 because no key "new" in prefrences

Related

Retrieve spinner value in SharedPreferences

i know i can save and get spinner val by this methods
but if i have long instead of int ,how should i do it
int userChoice = spinner.getSelectedItemPosition();
SharedPreferences sharedPref = getSharedPreferences("FileName",0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("userChoiceSpinner",usersChoice);
prefEditor.commit();
this to get
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);
}
in the same way you can use putLong and getLong
Your use the Integer.parseInt function on the spinner.getSelectedItemPosition result:
Integer.parseInt(spinner.getSelectedItemPosition())

How to store and retrieve integer using Android Shared Preferences

I am trying to save an integer value and retrieve the value using a same button using shared preferences.
To be more precise, when i click a button the value should be incremented(i++) and then it should be stored. When i close and open the application, it should retrieve the same value from where i left it. How do i do this?
I am using eclipse.
This works for me
public class OnPreferenceManager {
private SharedPreferences.Editor editor;
private SharedPreferences prefs;
private String startHour = "startHour";
private OnPreferenceManager() {}
private OnPreferenceManager(Context mContext) {
prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
editor = prefs.edit();
}
public static OnPreferenceManager getInstance(Context mContext)
{
OnPreferenceManager _app = null;
if (_app == null)
_app = new OnPreferenceManager(mContext);
return _app;
}
public void setStartHour(int hour){
editor.putInt(startHour, hour);
editor.apply();
}
public int getStartHour(){
int selectionStart = prefs.getInt(startHour, -1);
return selectionStart;
}
}
When you need to set integer just write as below
OnPreferenceManager.getInstance(this).setStartHour(theValueYouWantToStore);
And to retrieve write
OnPreferenceManager.getInstance(this).getStartHour()
You can use shared preferences as below.
//To save integer value
SharedPreferences preference = getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("YOU_KEY",you_int_value);
editor.commit();
//To retrieve integer value
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("YOU_KEY", 0); //0 is the default value
Check this gist https://gist.github.com/john1jan/b8cb536ca51a0b2aa1da4e81566869c4
I have created a Preference Utils class that will handle all the cases.
Its Easy to Use
Storing into preference
PrefUtils.saveToPrefs(getActivity(), PrefKeys.USER_INCOME, income);
Getting from preference
Double income = (Double) PrefUtils.getFromPrefs(getActivity(), PrefKeys.USER_INCOME, new Double(10));

Add data fromEeditText to double

I have an app where the user can enter an amount into an EditText but then I want to be able to add or subtract this to/from a double then be able to display this double with a TextView within another activity.
I'm not sure how to go about this and would appreciate some help.
Thanks in advance!
Edit: I forgot to mention that I also want this data to be kept between app launches/closes.
In your activity that accepts the input from the EditText:
double value = Double.parseDouble(yourEditText.getText().toString());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (adding) {
value = prefs.getFloat("your.float.key", 0f) + value;
} else {
value = prefs.getFloat("your.float.key", 0f) - value;
}
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("your.float.key", value);
editor.apply();
In your activity that shows the value:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Double value = prefs.getFloat("your.float.key", 0f);
yourTextView.setText(value.toString());
Firstly you will need to parse the data from your EditText, you can get a String from an EditText using
EditText.getText().toString()
and then use some form of
Double.parseDouble(String)
Integer.parseInt(String)
to get a numeric value from the string, with which you can then use for whatever math you need. After you calculate this value you will want to send it to another Activity via intent
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("KEY", myDouble);
startActivity(i);
to receive the intent in your next activity use
Bundle extras = getIntent().getExtras();
if (extras != null) {
Double myDouble = extras.getDouble("KEY");
}
and then if you want to save the value you will want to look into SharedPreferences
to save
SharedPreferences prefs = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("KEY", myFloat);
editor.commit();
to get
SharedPreferences prefs = getSharedPreferences("KEY", Context.MODE_PRIVATE);
myFloat = prefs.getFloat("KEY", myFloat);

Android: SharedPreferences Issue (Multiple SharedPreferences)

I have multiple SharedPreferences that are each storing one int value each. I have successfully created multiple SharedPreferences before, but I am trying a slightly different approach for this one. I am only keeping the highest 5 values. I am getting each value from its SharedPreference, then I am adding the 5 values + the current value I want to compare against the others in an ArrayList. I am calling the reverse sort method on it, then I am removing the last value (because it is extra). I am then putting each index into the editor of the SharedPreferences. Here is what I have:
prefs1 = this.getSharedPreferences("key1", Context.MODE_PRIVATE);
int value1 = prefs1.getInt("number1", 0);
prefs2 = this.getSharedPreferences("key2", Context.MODE_PRIVATE);
int value2 = prefs2.getInt("number2", 0);
prefs3 = this.getSharedPreferences("key3", Context.MODE_PRIVATE);
int value3 = prefs3.getInt("number3", 0);
prefs4 = this.getSharedPreferences("key4", Context.MODE_PRIVATE);
int value4 = prefs4.getInt("number4", 0);
prefs5 = this.getSharedPreferences("key5", Context.MODE_PRIVATE);
int value5 = prefs5.getInt("number5", 0);
ArrayList<Integer> numList = new ArrayList<Integer>();
Collections.addAll(numList, value0, value1, value2, value3, value4, value5);
Collections.sort(numList, Collections.reverseOrder());
numList.remove(numList.size()-1);
value1 = numList.get(0);
value2 = numList.get(1);
value3 = numList.get(2);
value4 = numList.get(3);
value5 = numList.get(4);
Editor editor = prefs1.edit();
editor.putInt("number1", value1);
editor.commit();
editor = prefs2.edit();
editor.putInt("number2", value2);
editor.commit();
editor = prefs3.edit();
editor.putInt("number3", value3);
editor.commit();
editor = prefs4.edit();
editor.putInt("number4", value4);
editor.commit();
editor = prefs5.edit();
editor.putInt("number5", value5);
editor.commit();
The problem I am having is that is showing 0s for each one of the values in my other activity even after value0 is positive after it executed through.
Is there anything wrong with how I am doing this? (If not then it must be when I get these values in another activity, but I am almost positive I have that right.)
EDIT*
Perhaps it is in the retrieval, here is from the retrieving activity:
prefs1 = this.getSharedPreferences("key1", Context.MODE_PRIVATE);
num1 = prefs1.getInt("number1", 0); //0 is the default value
tv1 = (TextView) findViewById(R.id.val1);
tv1.setText(String.valueOf(num1));
prefs2 = this.getSharedPreferences("key2", Context.MODE_PRIVATE);
num2 = prefs2.getInt("number2", 0); //0 is the default value
tv2 = (TextView) findViewById(R.id.val2);
tv2.setText(String.valueOf(num2));
prefs3 = this.getSharedPreferences("key3", Context.MODE_PRIVATE);
num3 = prefs3.getInt("number3", 0); //0 is the default value
tv3 = (TextView) findViewById(R.id.val3);
tv3.setText(String.valueOf(num3));
prefs4 = this.getSharedPreferences("key4", Context.MODE_PRIVATE);
num4 = prefs4.getInt("number4", 0); //0 is the default value
tv4 = (TextView) findViewById(R.id.val4);
tv4.setText(String.valueOf(num4));
prefs5 = this.getSharedPreferences("key5", Context.MODE_PRIVATE);
num5 = prefs5.getInt("number5", 0); //0 is the default value
tv5 = (TextView) findViewById(R.id.val5);
tv5.setText(String.valueOf(num5));
You can think of SharedPreferences like a giant map for all your stuff, but unless you are doing anything funky, you should store and retrieve data from the same giant map (i.e. the same SharedPreferences). What you are doing is creating named shared preferences. I would recommend just using the default. If you are curious to learn more about what that means check out this question.
So, in your case if you are storing 5 values, you can do so within the same SharedPreferences by just supplying different keys as follows:
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
mSharedPreferencesEditor.putInt("numberX", numberX);
mSharedPreferencesEditor.putInt("numberY", numberY);
mSharedPreferencesEditor.commit()
mSharedPreferences.getInt("numberX", numberX);
mSharedPreferences.getInt("numberY", numberY);
You can easily get and put with different keys into the same sharedPrefs. Your code would become:
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
int value1 = mSharedPreferences.getInt("number1", 0);
int value2 = mSharedPreferences.getInt("number2", 0);
int value3 = mSharedPreferences.getInt("number3", 0);
int value4 = mSharedPreferences.getInt("number4", 0);
int value5 = mSharedPreferences.getInt("number5", 0);
...
mSharedPreferencesEditor.putInt("number1", value1);
mSharedPreferencesEditor.putInt("number2", value2);
mSharedPreferencesEditor.putInt("number3", value3);
mSharedPreferencesEditor.putInt("number4", value4);
mSharedPreferencesEditor.putInt("number5", value5);
mSharedPreferencesEditor.commit();
Your issue concerning all your values being 0 may be different. I would fully expect all your calls to getInt to be 0 because you are never storing anything but 0 in the sharedPrefs. It looks like you are just adding zeros and putting zeros. I am not sure what you are trying to do here, but it sure would help to, at some point before calling this function, assign numbers 1-5 to something other than 0 by calling mSharedPreferencesEditor.putInt(number); on a sharedPrefs object like so:
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor mSharedPreferencesEditor = mSharedPreferences.edit();
//for example, for the key "number5"
mSharedPreferencesEditor.putInt("number5", value5);

SharedPreferences keep getting default value

I keep Getting the Default value either my UI will display null or if I use integers it displays that default value as well here it is in the string form plz help
//putting the information in shared preferences
TextView pScore1=(TextView)findViewById(R.id.pScore1f);
SharedPreferences peepsScores2= PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
SharedPreferences.Editor editor2 =peepsScores2.edit();
String userScore11 = pScore1.getText().toString();
editor2.putString("userScore11",userScore11);
editor2.commit();
//getting it and editing it
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
int u;
int one =1;
int newUsrScore1=1;
String userScore11 = peepsScores2.getString("userScore11",null);
u=Integer.parseInt(userScore11);
newUsrScore1 = u+one;
String newUserScore1 = Integer.toString(newUsrScore1);
SharedPreferences.Editor editor = peepsScores2.edit();
editor.putString(newUserScore1, NewUserScore1);
editor.commit();
//getting it and displaying it on the UI
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
String userScore11 = peepsScores2.getString("NewuserScore1",null);
pScore1.setText(" "+userScore11);
I have added some comment to you code please check:
//putting the information in shared preferences
TextView pScore1=(TextView)findViewById(R.id.pScore1f);
SharedPreferences peepsScores2=
PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
SharedPreferences.Editor editor2 =peepsScores2.edit();
String userScore11 = pScore1.getText().toString();
editor2.putString("userScore11",userScore11);
editor2.commit();
//getting it and editing it
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
int u;
int one =1;
int newUsrScore1=1;
String userScore11 = peepsScores2.getString("userScore11",null);
u=Integer.parseInt(userScore11);
newUsrScore1 = u+one;
String newUserScore1 = Integer.toString(newUsrScore1);
SharedPreferences.Editor editor = peepsScores2.edit();
//#Praful: here newUserScore1 seems to be integer value and you are storing
//null here. I think it it should be
//`editor.putString("NewuserScore1", newUsrScore1);`
editor.putString(newUserScore1, null);
//#Praful: call commit here
editor.commit;
//getting it and displaying it on the UI
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
String userScore11 = peepsScores2.getString("NewuserScore1",null);
pScore1.setText(" "+userScore11);
This line
editor.putString(newUserScore1, null);
should be
editor.putString("NewuserScore1",newUserScore1);
and also don't forget to commit your changes using editor.commit();
Whenever you working with SharedPreference never forget to call commit() to save your changes.
SharedPreferences.Editor editor = peepsScores2.edit();
editor.putString("NewuserScore1", newUserScore1);
editor.commit();

Categories

Resources