ESM
ESM

Reputation: 183

Shared Preferences no longer saving

I have an activity that saves ListView objects to an ArrayList and loads them upon OnCreate.

I tested it out last night and it worked perfectly, however when I tested it again today, it's loading what it already has saved from yesterday, but it is no longer saving new list items.

Here's my code:

public class SubActivity extends AppCompatActivity {

Button addItem;
ImageButton back;
Button saveListBtn;
private ListView listViewer;
ArrayList<ItemObjects> items = new ArrayList<ItemObjects>();
private String key = "arg";

//Retrieves the saved preferences from the given kay: value pair.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment1_item_screen);
    Bundle extra = getIntent().getExtras();
    String extraString = extra.getString(key);

//This loads the items from sharedPreferences and is working correctly.

    if(loadList(extraString) != null){
        items = loadList(extraString);
    }

//Loads the listView with the correct information.

    final ItemObjectsAdapter adapter = new ItemObjectsAdapter(this, 
 items);
    listViewer = (ListView) findViewById(R.id.itemListView);
    listViewer.setAdapter(adapter);

//At the push of a button, a new object is added to the items ArrayList. I'm just putting in a blank object for now. The issues comes at saveList(items, key). It doesn't seem to be working correctly, but it did yesterday, since every time I load the activity, 4 list items are loaded from sharedPreferences, however, I cannot seem to save anymore.

    addItem = (Button) findViewById(R.id.addItemBtn);
    addItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ItemObjects blank = new ItemObjects("", "", "", "");
            items.add(blank);
            saveList(items, key);
            adapter.notifyDataSetChanged();
        }
    });

//Here's the code I have for saving the items list. Note this worked yesterday, I haven't changed anything with it yet.

private void saveList(ArrayList<ItemObjects> list, String key) {
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this.getApplicationContext());
    SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(list);
    prefsEditor.putString(key, json);
    prefsEditor.apply();
}

//This is just the code to load the items from sharedPreferences and seems to be working correctly.

private ArrayList<ItemObjects> loadList(String key) {
    SharedPreferences prefs = 
PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
    Gson gson = new Gson();
    String json = prefs.getString(key, null);
    Type type = new TypeToken<ArrayList<ExerciseObjects>>() {}.getType();
    return gson.fromJson(json, type);
}
}

Any ideas as to why it's not saving?

Upvotes: 0

Views: 36

Answers (1)

ESM
ESM

Reputation: 183

Ok so looks like saveList(items, key); is supposed to be saveList(items, extraString);

It works now.

Upvotes: 0

Related Questions