0

I have two buttons and one textview in each child row. those two buttons are used as triggers to count number and then write the number in textview. My problem is, the value of textview is always changes when i scroll the recyclerview and sometimes affect the textview on neighbour child row. anyone can help ? thank you.

when i click + on first row (1), textview on third row(2) is also changed.

when i click + on first row (1), textview on third row(2) is also changed, and when i scroll down, those numbers are both missing.

i used this library: https://github.com/thoughtbot/expandable-recycler-view

Here is my code:

MenuDeliveryRecyclerAdapter.java

public abstract class MenuDeliveryRecyclerAdapter extends ExpandableRecyclerViewAdapter<MenuDeliveryRecyclerAdapter.MenuViewHolder, MenuDeliveryRecyclerAdapter.SubmenuViewHolder> {
    List<MenuItemListModel> list;
    private LayoutInflater mInflator;
    private Context context;

    public MenuDeliveryRecyclerAdapter(Context context, @NonNull List<? extends ExpandableGroup> parentItemList) {
        super(parentItemList);
        this.context = context;
        mInflator = LayoutInflater.from(context);
        Timber.e("MenuItemListModel: %s", parentItemList.toString());
    }

    public void setData(List<MenuItemListModel> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public MenuViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        View view = mInflator.inflate(R.layout.item_category, parent, false);
        return new MenuViewHolder(view);
    }

    @Override
    public SubmenuViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View view = mInflator.inflate(R.layout.item_menu_reservasi, parent, false);
        return new SubmenuViewHolder(view);
    }

    @Override
    public void onBindChildViewHolder(SubmenuViewHolder holder, int flatPosition, ExpandableGroup group,
                                  int childIndex) {
        final MenuItemResponseModel menu = ((DeliveryExpandableModel) group).getItems().get(childIndex);
        holder.bind(menu , childIndex);
    }

    @Override
    public void onBindGroupViewHolder(MenuViewHolder holder, int flatPosition,
                                  ExpandableGroup group) {
        holder.category.setText(group.getTitle());
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    public class MenuViewHolder extends GroupViewHolder {

        private TextView category;

        public MenuViewHolder(View itemView) {
            super(itemView);
            category = (TextView) itemView.findViewById(R.id.category);
        }
    }

    public class SubmenuViewHolder extends ChildViewHolder {

        private ImageView gambarmenu;
        private TextView namamenu;
        private TextView hargamenu;
        private Button count;
        private Button btnMin;
        private Button btnPlus;

        public SubmenuViewHolder(View convertView) {
            super(convertView);
            gambarmenu = (ImageView) convertView.findViewById(R.id.gambarmenu);
            namamenu = (TextView) convertView.findViewById(R.id.namamenu);
            hargamenu = (TextView) convertView.findViewById(R.id.hargamenu);

            count = (Button) convertView.findViewById(R.id.count);
            btnMin = (Button) convertView.findViewById(R.id.btnMin);
            btnPlus = (Button) convertView.findViewById(R.id.btnPlus);
        }

        public void bind(final MenuItemResponseModel childModel, final int childIndex) {
            Glide.with(context).load(childModel.getGambar())
                .thumbnail(0.5f)
                .crossFade()
                .into(gambarmenu);

            namamenu.setText(childModel.getNama_menu());
            hargamenu.setText(Utils.formatKursIndonesia(Double.valueOf(childModel.getHarga())));

            btnMin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int jlh = Integer.valueOf(count.getText().toString());
                    count.setText(String.valueOf(jlh - 1 >= 0 ? jlh - 1 : 0));
                    notifyDataSetChanged();
                    //notifyItemChanged(childIndex);
                    onLeftMenuClick(childModel, jlh);
                }
            });

            btnPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int jlh = Integer.valueOf(count.getText().toString());
                    count.setText(String.valueOf(jlh+1));
                    notifyDataSetChanged();
                    //notifyItemChanged(childIndex);
                    onRightMenuClick(childModel, jlh);
                }
            });
        }
    }

    protected abstract void onRightMenuClick(MenuItemResponseModel parent, int jumlah);

    protected abstract void onLeftMenuClick(MenuItemResponseModel parent, int jumlah);
}
klaudiusnaban
  • 196
  • 1
  • 10

0 Answers0