Guest User

Untitled

a guest
May 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  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
Add Comment
Please, Sign In to add comment