将项目添加到收藏夹数据库时更改图标颜色

当我单击心形图标时,它会将项目发送到收藏夹并更改颜色,但是当加载活动时,图标会恢复正常颜色,但该项目仍在收藏夹中。


我如何检查收藏夹中的项目是否基于数据库房间的监听器之类的东西更改图标颜色?


这是适配器:-


    public class BSAdapter extends RecyclerView.Adapter<BSAdapter.BestSellerHolder> {

    private Context context;

    private List<ProductsBestSeller> bestSellerList;



    public BSAdapter(Context context, List<ProductsBestSeller> bestSellerList) {

        this.context = context;

        this.bestSellerList = bestSellerList;

    }


    @Override

    public BestSellerHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View bestSellerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.default_product_items_horizontal, parent, false);

        return new BestSellerHolder(bestSellerView);

    }


    @Override

    public void onBindViewHolder(BestSellerHolder holder, int position) {

        ProductsBestSeller bestSeller = bestSellerList.get(position);

        holder.onBindData(bestSeller);


    }


    @Override

    public int getItemCount() {

        if (bestSellerList != null) {

            return bestSellerList.size();

        } else {

            return 0;

        }

    }


    public class BestSellerHolder extends RecyclerView.ViewHolder {


        TextView productName, productPrice;

        ImageView productIcon;

        CheckBox favouriteIcon;



        public BestSellerHolder(View itemView) {

            super(itemView);

            productIcon = itemView.findViewById(R.id.product_icon_horizontal);

            productName = itemView.findViewById(R.id.product_name_horizontal);

            productPrice = itemView.findViewById(R.id.product_price_horizontal);

            favouriteIcon = itemView.findViewById(R.id.favourite_icon_horizontal);

        }


largeQ
浏览 105回答 1
1回答

森栏

我可以在上面的代码中看到 2 个问题,当我们添加和删除时您正在更新数据库,这很好,但是您处理本地视图引用的方式是错误的。原因:因为在你的情况下,不仅当你转到另一个屏幕时它不会工作,如果你滚动更多如果你有更多的项目然后回来它也不会工作,因为回收视图重用你更新的视图在检查监听器中,这导致了第二个问题在 onBindData 中,你总是应该使用非收藏图标,所以每当你滚动和查看重用它时,它只会显示非收藏图标,你应该检查该项目是否是收藏,你应该更新视图例如,你应该像下面那样override fun onBindViewHolder(holder: VM, position: Int) {&nbsp; &nbsp; val item = items.get(position)&nbsp; &nbsp; if (item.favourite == 0) {&nbsp; &nbsp; &nbsp; &nbsp; holder.name.text = item.name&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; holder.name.text = item.name + " Liked "&nbsp; &nbsp; }&nbsp; &nbsp; holder.favouriteIcon.setOnCheckedChangeListener { compoundButton, isChecked ->&nbsp; &nbsp; &nbsp; &nbsp; // Should not update local view reference here&nbsp; &nbsp; &nbsp; &nbsp; if(isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Update the local reference object, Just not to update from DB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.favourite = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do the logic to update the DB to add the item in Fav&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Update the local reference object, Just not to update from DB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.favourite = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do the logic to update to remove the item from Fav list&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; notifyItemChanged(position) // Helps to update the particular item&nbsp; &nbsp; }}请根据您的项目修改代码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java