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