0

Friends I want to store array list in shared prefrences file through json and want to load all previous stored data.

When I use Hard Code using multiple class objects it stores in json xml file. and when i store a class object it override previous data.

here is my code

public void save(){

    StudentData data1 = null;

    SharedPreferences sharedPreferences = getSharedPreferences(FileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    list = new ArrayList<>();


    data1 = addEmployee();
    list.add(data1);


    Gson  gson = new  Gson();
    String jsonPref = gson.toJson(list);

    editor.putString(PrefKey ,jsonPref );
    editor.apply();

    if (editor.commit()) {
        clear();
        Toast.makeText(getApplicationContext(), "Saved ! "+jsonPref, Toast.LENGTH_SHORT).show();
        startActivity(new Intent(SaveForm.this,MainActivity.class));
    }else {
        Toast.makeText(getApplicationContext(), "Data Not Saved", Toast.LENGTH_SHORT).show();
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122

0 Answers0