-1

I'm working on an application for a barber shop and I have a list of orders some of them for adults and other for children so when the user chooses a service for a person and presses Add I show the the price but whenever the list grows and I remove some person from the middle all the prices which follows get back to zero I think that's because I begin the holder with a zero but what I want is whenever I remove an item all other items doesn't get recycled

ViewHolder

public class OrderDetailViewHolder extends RecyclerView.ViewHolder    {
    private static final String TAG = "OrderDetailViewHolder";
    public Button offersBtn,servicesBtn;
    public TextView priceTxt,personTxt;
    IServer server;
    Context mContext;
ChainedCallbackForHairdresserTotal chainedCallbackForHairdresserTotal;

    public void setChainedCallbackForHairdresserTotal(ChainedCallbackForHairdresserTotal chainedCallbackForHairdresserTotal) {
        this.chainedCallbackForHairdresserTotal = chainedCallbackForHairdresserTotal;
    }

    ArrayList<String>currentServices=new ArrayList<>();
    HashMap<String,String> currentOffers=new HashMap<>();

    ArrayList<Models>servicesAdult=new ArrayList<>();
    ArrayList<Models>servicesChild=new ArrayList<>();
    ArrayList<Models>offersAdult=new ArrayList<>();
    ArrayList<Models> offersChild=new ArrayList<>();
    ArrayList<Models>DialogArray=new ArrayList<>();

        public OrderDetailViewHolder(@NonNull View itemView, final Context mContext) {
        super(itemView);
        offersBtn=itemView.findViewById(R.id.offersBtn);
        servicesBtn=itemView.findViewById(R.id.servicesBtn);
        priceTxt=itemView.findViewById(R.id.priceTxt);
        personTxt=itemView.findViewById(R.id.personTxt);
        this.mContext=mContext;
        Fonts fonts=new Fonts(mContext);
        fonts.setTypeFce(itemView);
        server=Common.getAPI();

loadServices();



        Log.d(TAG, "OrderDetailViewHolder: ");

        }

    private void loadServices() {
        Log.d(TAG, "loadServices: currentSaloon.getSaloonId() "+Common.currentSaloon.getSaloonId());
        server.getServices(String.valueOf(Common.currentSaloon.getSaloonId())).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d(TAG, "onResponse: "+response.body());
                Parser parser=new Parser(mContext);
                parser.parse(response.body());
                Log.d(TAG, "onResponse: "+parser.getServices().size());
                getSaloonServicesAndOffers(parser.getServices());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }



    public void setListeners(final Models orderDetailModel){
        servicesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // show Services Dialog

                String orderType=orderDetailModel.getOrderPersonType();
                if(orderType.equals("child")){
                    DialogArray=servicesChild;
                }
                else if(orderType.equals("adult")){
                    DialogArray=servicesAdult;
                }
                ListsDialog listsDialog=new ListsDialog(mContext,Common.DIALOG_LAYOUT_TYPE_SERVICE,getAdapterPosition(),DialogArray,null,"",0,currentServices,currentOffers);
                listsDialog.setOnPersonTotalComputed(new onPersonTotalComputed() {
                    @Override
                    public void onPersonTotalComputed(float personTotal) {
                        priceTxt.setText(String.valueOf((int)personTotal)+"EGP");
                  chainedCallbackForHairdresserTotal.getTotal(personTotal,true);
                        if(personTotal==0){
                            priceTxt.setText("0EGP");
                        }
                    }
                });
                listsDialog.show();

            }
        });

        offersBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // show Services Dialog
                String orderType=orderDetailModel.getOrderPersonType();
                if(orderType.equals("child")){
                    DialogArray=offersChild;
                }
                else if(orderType.equals("adult")){
                    DialogArray=offersAdult;
                }
    ListsDialog listsDialog=new ListsDialog(mContext,Common.DIALOG_LAYOUT_TYPE_OFFER,getAdapterPosition(),DialogArray,null,"",0,currentServices,currentOffers);
                listsDialog.setOnPersonTotalComputed(new onPersonTotalComputed() {
                    @Override
                    public void onPersonTotalComputed(float personTotal) {
                        priceTxt.setText(String.valueOf((int)personTotal)+"EGP");
                        chainedCallbackForHairdresserTotal.getTotal(personTotal,true);
                        if(personTotal==0){
                            priceTxt.setText("0EGP");
                        }
                    }
                });

                listsDialog.show();

            }
        });
    }



    public void bind(Models orderDetailModel, int pos) {

            

    personTxt.setText(orderDetailModel.getOrderPersonType().equals("adult")? mContext.getString(R.string.adult)+String.valueOf(orderDetailModel.getOrderId())
            :mContext.getString(R.string.child)+String.valueOf(orderDetailModel.getOrderId()));

    priceTxt.setText("0EGP");
    setListeners(orderDetailModel);




    }

here's1 when the list only has 3 adults everything is fine and then when I added a child2 also all is fine then I added an adult#4 which has a 40 price but when I removed child#1 the price of adult#4 went back to zero 3 how to prevent that ?

Community
  • 1
  • 1
hagar Mohamad
  • 97
  • 1
  • 11

2 Answers2

0

It is against all logic that RecyclerView was made. Holders has to be recycled.

All data that is fetched you can store in list of adapter, because adapter holds reference of it and you can access it form ViewHolder.

Also best practice would be do all async logic outside ViewHolder

Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
0

the answer is whenever you remove an item just write notifyItemRemoved(position); this won't recreate the next viewholders in the list instead of notifyItemRangeChanged(position, orders.size()); this will recreate the next items

hagar Mohamad
  • 97
  • 1
  • 11