问题
I am trying delete View with Swipe in RecylerView
, I saved in SharedPreferences
to selected View with favorite button. After I saved to selected View in SharedPreferences
, I am trying remove Favorite Activity
with Swipe to left ,I was done this but When I return Favorite Activity
, I saw old items doesn't updated SharedPreferences
When I swipe to left.
How can I do this?
public class SharedPreference {
public static final String PREFS_NAME = "NKDROID_APP";
public static final String FAVORITES = "Favorite";
public SharedPreference() {
super();
}
public void storeFavorites(Context context, List<OrderModel> favorites) {
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
editor = settings.edit();
Gson gson = new Gson();
String jsonFavorites = gson.toJson(favorites);
editor.putString(FAVORITES, jsonFavorites);
editor.commit();
}
public ArrayList<OrderModel> loadFavorites(Context context) {
SharedPreferences settings;
List<OrderModel> favorites;
settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
Gson gson = new Gson();
OrderModel[] favoriteItems = gson.fromJson(jsonFavorites,OrderModel[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList<OrderModel>(favorites);
} else
return null;
return (ArrayList<OrderModel>) favorites;
}
public void addFavorite(Context context, OrderModel beanSampleList) {
List<OrderModel> favorites = loadFavorites(context);
if (favorites == null)
favorites = new ArrayList<OrderModel>();
favorites.add(beanSampleList);
storeFavorites(context, favorites);
}
public void removeFavorite(Context context, OrderModel beanSampleList) {
ArrayList<OrderModel> favorites = loadFavorites(context);
if (favorites != null) {
favorites.remove(beanSampleList);
storeFavorites(context, favorites);
}
}
/*
public void saveHighScoreList(String scoreString) {
editor.putString(FAVORITES, scoreString);
editor.commit();
}
public String getHighScoreList() {
return settings.getString(FAVORITES, "");
}
*/
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof OrderAdapter.OrderViewHolder) {
// get the removed item name to display it in snack bar
String name = order_models.get(viewHolder.getAdapterPosition()).getOrder_name();
final OrderModel deletedItem = order_models.get(viewHolder.getAdapterPosition());
final int deletedIndex = viewHolder.getAdapterPosition();
order_adapter.removeItem(viewHolder.getAdapterPosition());
//remove from shared preferences
sharedPreference.removeFavorite(Orders.this, deletedItem);
order_models.remove(deletedItem);
order_adapter.notifyDataSetChanged();
Toast.makeText(Orders.this, "Success Remove",Toast.LENGTH_SHORT).show();
// showing snack bar with Undo option
Snackbar snackbar = Snackbar
.make(constraint, name + " removed from cart!", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
// undo is selected, restore the deleted item
order_adapter.restoreItem(deletedItem, deletedIndex);
}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
}
回答1:
For one, you probably shouldn't be loading the list of favorites from preferences every single time you want to query or modify the list. Instead, query it once when the Activity this RecyclerView belongs to is created (you could do that from the Adapter itself or from the Activity), and store that to a global variable. ie:
class SomeActivity extends Activity {
private ArrayList<OrderModel> favorites = new ArrayList<>();
private SharedPreference prefsHelper = new SharedPreference();
@Override
public void onCreate(Bundle savedInstanceState) {
//....
favorites.addAll(prefsHelper.loadFavorites(this));
}
}
Then, when you want to change something, modify that ArrayList and then save it directly:
public void addFavorite(OrderModel model) {
favorites.add(model);
prefsHelper.storeFavorites(this, favorites);
}
You'll probably need to modify this to fit your code, but it's an example of what to do.
What you currently have won't work, because every time you go to modify the list, you're recreating it from a String representation. That means that the list of favorites you have loaded contains completely different instances of the models, even if they contain the same values.
When you pass an OrderModel to your removeFavorite()
method, it's not going to remove anything, because nothing is equal; by reloading the list, you have completely fresh instances.
If you really want to keep your current code structure, switch to indexes instead of passing the object. Or, override equals()
in OrderModel and have it manually compare the values, so even different instances can be matched.
来源:https://stackoverflow.com/questions/52207263/when-deleting-view-with-swipe-in-recyclerview-doesnt-deletin-in-sharedpreferenc