0

I have an app in which I send server request and in response server gives me some data, At this point I want to store in SharedPreference.

In next server request I again find the length of data coming in response from server, at this point of time I want this length to add with previous save value in preference and same process again and again.

Code for finding server response length:

JSONArray posts = response.optJSONArray("data");
int arrayCount = posts.length();
Aspicas
  • 4,483
  • 4
  • 29
  • 53
Satish
  • 151
  • 1
  • 1
  • 8

2 Answers2

1

In order to use shared preferences , you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

You can save something in the sharedpreferences by using SharedPreferences.Editor class. You will call the edit method of SharedPreference instance and will receive it in an editor object. Its syntax is −

Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();

To fetch data Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
siddhesh
  • 1,365
  • 1
  • 10
  • 16
  • how to add new value with earlier saved in shared preference – Satish Sep 16 '16 at 07:52
  • I want to add new data with previous data saved in preference i.e suppose 1st data saved in preference is 1, in second time i want to retreive this value and add with 2 and saved in shared preference – Satish Sep 16 '16 at 07:54
  • then retrieve data add your new content and commit it with same key – siddhesh Sep 16 '16 at 07:56
  • sample example plssss – Satish Sep 16 '16 at 07:58
  • SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("text", null); restoredText+="Your Content"; Editor editor = sharedpreferences.edit(); editor.putString("text", restoredText); editor.commit(); – siddhesh Sep 16 '16 at 08:54
0
// write
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("length", arrayCount);
editor.apply();

// read
SharedPreferences prefs = getSharedPreferences(PREF_FILE_NAME,MODE_PRIVATE);
int length = prefs.getInt("length", 0);

If you want to add length value to previous stored value incrementally, do something like this:

JSONArray posts = response.optJSONArray("data");
int arrayCount = posts.length();

// read stored value
SharedPreferences prefs = getSharedPreferences(PREF_FILE_NAME,MODE_PRIVATE);
int length = prefs.getInt("length", 0);

// save incremental length
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("length", length + arrayCount);
editor.apply();
Aspicas
  • 4,483
  • 4
  • 29
  • 53
lubilis
  • 3,667
  • 4
  • 31
  • 51