Estoy tratando de guardar el estado de un botón dentro de un adaptador con SharedPrederences
en Android
, parcialmente lo he conseguido pero, resulta que el cambio se ejecuta en todos los botones, adjunto una imagen para explicarme mejor.
La pantalla principal es un Fragment
que se muestra dentro del MainActivity
, trate de utilizar el método add() en lugar del replace() pero, no solucionaría que al cerrar la aplicación se mantengan los estados del botón, de antemano muchas gracias por su tiempo.
public class RecipesAdapter extends
RecyclerView.Adapter<RecipesAdapter.RecipesViewHolder> {
View view;
Context context;
List<RecipesVo> listRecipes;
//RecyclerItemClick itemClick;
IComunicaFragments itemClick;
public RecipesAdapter(List<RecipesVo> listRecipes, IComunicaFragments itemClick, Context context) {
this.listRecipes = listRecipes;
this.itemClick = itemClick;
this.context = context;
}
@NonNull
@Override
public RecipesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_recipe, parent, false);
return new RecipesViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecipesViewHolder holder, int position) {
//declare recyclerview false to recycle
//holder.setIsRecyclable(false);
//declare list position
RecipesVo recipes = listRecipes.get(position);
//declare list fav
//declare connection to fill elements
holder.imgThumbnail.setImageResource(recipes.getIdThumbnail());
holder.txtRecipeName.setText(recipes.getRecipeName());
holder.txtRecipeTime.setText(recipes.getTimePrepare());
holder.txtRecipeDifficult.setText(recipes.getDifficultPrepare());
//declare button fav and state
setButtonStatus(holder, recipes);
//implement the onClick interface
holder.itemView.setOnClickListener(v -> {
//itemClick.itemClick(recipes);
itemClick.sendRecipe(recipes);
});
}
private void setButtonStatus(RecipesViewHolder holder, RecipesVo recipes) {
SharedPreferences preferences = context.getSharedPreferences(Utilities.PREFS_KEY, Context.MODE_PRIVATE);
recipes.setSelected(preferences.getBoolean(Utilities.BOOLEAN_KEY, Boolean.parseBoolean("")));
if (recipes.getSelected()) {
holder.btnAddFav.setImageResource(R.drawable.ic_btn_fav_selected);
} else {
holder.btnAddFav.setImageResource(R.drawable.ic_btn_fav_unselected);
}
holder.btnAddFav.setOnClickListener(v -> {
//change background button
pressedBtn(holder, recipes);
});
}
private void pressedBtn(RecipesViewHolder holder, RecipesVo recipes) {
SharedPreferences preferences = context.getSharedPreferences(Utilities.PREFS_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor objEditor = preferences.edit();
if (!recipes.getSelected()) {
recipes.setSelected(true);
holder.btnAddFav.setImageResource(R.drawable.ic_btn_fav_selected);
//Toast.makeText(view.getContext(), "Added:", Toast.LENGTH_SHORT).show();
itemClick.keepFav(recipes);
//saveRecipe(recipes);
} else {
recipes.setSelected(false);
holder.btnAddFav.setImageResource(R.drawable.ic_btn_fav_unselected);
//Toast.makeText(view.getContext(), "Deleted:", Toast.LENGTH_SHORT).show();
itemClick.deleteFav(recipes);
//deleteRecipe(recipes);
}
objEditor.putBoolean(Utilities.BOOLEAN_KEY, recipes.getSelected());
objEditor.apply();
}
@Override
public int getItemCount() {
return listRecipes.size();
}
public static class RecipesViewHolder extends RecyclerView.ViewHolder {
//declare items to click
CardView cardRecipe;
ImageButton btnAddFav;
//declare items to fill
TextView txtRecipeName, txtRecipeTime, txtRecipeDifficult;
ImageView imgThumbnail;
public RecipesViewHolder(@NonNull View itemView) {
super(itemView);
//items onclick
cardRecipe = itemView.findViewById(R.id.card_recipes);
btnAddFav = itemView.findViewById(R.id.btn_fav_list);
//items to fill
imgThumbnail = itemView.findViewById(R.id.img_thumbnail);
txtRecipeName = itemView.findViewById(R.id.txt_recipe_name_list);
txtRecipeTime = itemView.findViewById(R.id.txt_time_list);
txtRecipeDifficult = itemView.findViewById(R.id.txt_prepare_list);
}
}
}