I am sending data from ItemDetails class to OrdersFragment using shared preferences. I receive the data successfully, but just the last item, it's overwritting item in the cart. How can I update the preferences I send from ItemDetails with new values?
This is my ItemDetails class:
btn_AddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
Gson gson = new Gson();
// send names
ArrayList<String> namesList = new ArrayList<>();
namesList.add(name);
String namesJson = gson.toJson(namesList);
sharedPreferences.edit().putString("names", namesJson).apply();
// send prices
ArrayList<String> pricesList = new ArrayList<>();
pricesList.add(price);
String pricesJson = gson.toJson(pricesList);
sharedPreferences.edit().putString("prices", pricesJson).apply();
}
});
And this is OrdersFragment:
SharedPreferences sharedPreferences = context.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
Gson gson = new Gson();
// get names
ArrayList<String> namesList = new ArrayList<>();
String namesJson = sharedPreferences.getString("names", null);
String[] names = gson.fromJson(namesJson, String[].class);
if (names != null) {
namesList.clear();
namesList.addAll(Arrays.asList(names));
}
// get prices
ArrayList<String> pricesList = new ArrayList<>();
String pricesJson = sharedPreferences.getString("prices", null);
String[] prices = gson.fromJson(pricesJson, String[].class);
if (prices != null) {
pricesList.clear();
pricesList.addAll(Arrays.asList(prices));
}
RecyclerView rvOrdered = view.findViewById(R.id.rv_ordered);
rvOrdered.setLayoutManager(new LinearLayoutManager(context));
rvOrdered.setHasFixedSize(true);
//populate the orders list
ArrayList<OrderedModel> orders = new ArrayList<>();
for(int i = 0; i < namesList.size(); i++) {
orders.add(new OrderedModel(namesList.get(i), pricesList.get(i)));
}
rvOrdered.setAdapter(new OrderedRecyclerViewAdapter(context, orders));
How can I update the list in ItemDetails and get all of the orders in OrdersFragment instead of one? Can I use multiple shared preferences or? I believe I have to fetch the data from fragment first and then just append the new values I guess?
First get the sharedPreferences using this:
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String namesJson = sharedPreferences.getString("names", "");
String[] names = gson.fromJson(namesJson, String[].class);
Then make a new array and add all the old names plus the new name:
ArrayList<String> namesArray = new ArrayList<>();
for(String n : names) {
namesArray.add(n);
}
namesArray.add(name); // add the new name here
Then save it again in ItemDetails before sending it over:
sharedPreferences.edit().putString("names", gson.toJson(namesArray)).apply();
Now you can access all the names, old and new, in OrdersFragment. Do the same for "prices", it works the same.