daily pastebin goal
71%
SHARE
TWEET

Untitled

a guest May 23rd, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SharedPreferences preferences =
  2. getSharedPreferences("mypref",MODE_PRIVATE);
  3.  
  4.     SharedPreferences.Editor prefsEditor=preferences.edit();
  5.     Gson gson=new Gson();
  6.  
  7.     if(preferences.contains("counter")){
  8.         counter=preferences.getInt("counter",0);
  9.         prefsEditor.putInt("counter",++counter);
  10.  
  11.     }
  12.     else{
  13.         counter=0;
  14.         prefsEditor.putInt("counter",++counter);
  15.     }
  16.  
  17.     String json = gson.toJson(student);
  18.     prefsEditor.putString("details"+counter,json);
  19.     prefsEditor.commit();
  20.  
  21.     startActivity(intent);
  22.    
  23. SharedPreferences mPrefs =
  24.                     getSharedPreferences("mypref",MODE_PRIVATE);
  25.     int howMany=mPrefs.getInt("counter",0);
  26.     Gson gson = new Gson();
  27.     for(int i=1;i<=howMany;i++) {
  28.         String json = mPrefs.getString("details" + howMany, "");
  29.         Student student = gson.fromJson(json, Student.class);
  30.         Log.e("tag","loop number"+i);
  31.         Log.e("Tag", student.getName());
  32.     }
  33.    
  34. 05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent
  35. E/tag: loop number1
  36. 05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent
  37. E/Tag: suv
  38. 05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent
  39. E/tag: loop number2
  40. 05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent
  41. E/Tag: suv
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top