How to show total amount from recyclerview list android java

Viewed 18

I am using recycler view where it has edit text and spinner based upon the spinner value and the edit text value the amount will be shown in every list. When I add new list and tired to change the value in the list amount is not getting updated. '''

public class GiftVoucherPersonalAdapter extends RecyclerView.Adapter<GiftVoucherPersonalAdapter.ViewHolder> {

    Context context;
    ViewHolder viewHolder;
    String amount, noOfVoucher, receiversMail, txtFrom, txtTo, txtMessage;
    IGetDetails iGetDetails;
    List<String> amountText;
    List<String> amountVal;
    CustomSpinnerAdapter customSpinnerAdapterAmount;
    List<GiftVoucherModel> giftVoucherList;
    Map<Integer, Integer> selectedSpinnerValue = new HashMap<Integer, Integer>();
    GiftVoucherAmountChangeListener giftVoucherAmountChangeLstener;
    int totalQuantity = 0, totalvalue = 0;

    public GiftVoucherPersonalAdapter(Context context, List<GiftVoucherModel> giftVoucherList, IGetDetails iGetDetails, final List<String> amtText, final List<String> amtVal, GiftVoucherAmountChangeListener giftVoucherAmountChangeLstener) {
        this.context = context;
        this.iGetDetails = iGetDetails;
        this.amountText = amtText;
        this.amountVal = amtVal;
        this.giftVoucherAmountChangeLstener = giftVoucherAmountChangeLstener;
        this.giftVoucherList = giftVoucherList;


    }


    public void updateList(List<GiftVoucherModel> giftVoucherList) {
        this.giftVoucherList = giftVoucherList;


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_personal_gift, viewGroup, false);

        return new GiftVoucherPersonalAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int listposition) {
        //show close icon
        if (listposition == 0) {
            viewHolder.imgCloseForm.setVisibility(View.GONE);
        } else {
            viewHolder.imgCloseForm.setVisibility(View.VISIBLE);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                giftVoucherAmountChangeLstener.onAmountChange(giftVoucherList);
            }
        });
        //
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    private void updateGfList(List<GiftVoucherModel> gfModel, int listposition) {

        giftVoucherList.set(listposition, gfModel.get(listposition));

        //notifyDataSetChanged();

    }

    @Override
    public int getItemCount() {
        return giftVoucherList.size();

    }

    public void getValues() {

        receiversMail = viewHolder.txtRecMailAddress.getText().toString().trim();
        noOfVoucher = viewHolder.txtNoOfVouc.getText().toString().trim();
        if (viewHolder.lblPersonaliseCheck.isChecked()) {
            txtTo = viewHolder.txtTo.getText().toString().trim();
            txtFrom = viewHolder.txtFrom.getText().toString().trim();
            txtMessage = viewHolder.txtMessage.getText().toString().trim();
            iGetDetails.getDetails(receiversMail, amount, noOfVoucher, true, txtTo, txtFrom, txtMessage);
        } else {
            txtTo = "";
            txtFrom = "";
            txtMessage = "";
            iGetDetails.getDetails(receiversMail, amount, noOfVoucher, false, txtTo, txtFrom, txtMessage);
        }


    }

    public void removeFor(int pos) {
        Log.d("remove pos", String.valueOf(pos) + "listsize" + giftVoucherList.size());
        giftVoucherList.remove(pos);
        notifyItemRemoved(pos);
        notifyItemRangeChanged(pos, giftVoucherList.size());
        Log.d("remove pos", String.valueOf(giftVoucherList.size()));
        notifyDataSetChanged();
        updateList(giftVoucherList);
//        if(giftVoucherList.size()>0) {
//            giftVoucherList.remove(giftVoucherList.size() - 1);
//            studentAdapter.notifyDataSetChanged();
//            Toast.makeText(MainActivity.this, String.valueOf(studentDataList.size()), Toast.LENGTH_LONG).show();
//        }

    }

    public int getspinnervalueposition(float selectedvalue) {
        for (int position = 0; position < amountVal.size(); position++)
            if (Float.parseFloat(amountVal.get(position)) == selectedvalue)
                return position;
        return 0;
    }

    public int getMyTotalQuantity() {

        for (int i = 0; i < giftVoucherList.size(); i++) {
            if (giftVoucherList.get(i) != null && giftVoucherList.get(i).getGfTotal() != null &&
                    !TextUtils.isEmpty(giftVoucherList.get(i).getGfTotal())) {
                totalQuantity = totalQuantity + Integer.parseInt(giftVoucherList.get(i).getGfTotal());
            }
            return totalQuantity;
        }
        return 0;
    }

    public interface IGetDetails {
        void getDetails(String recieversMail, String amount, String NoOfVouchers, boolean isPersonaliseChecked, String to, String From, String Message);


    }

    public interface GiftVoucherAmountChangeListener {
        void onAmountChange(List<GiftVoucherModel> giftVoucherModels);

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txtRecMailAddress)
        EditText txtRecMailAddress;
        @BindView(R.id.txtNoOfVouc)
        EditText txtNoOfVouc;
        @BindView(R.id.lblgcTotalValue)
        TextView lblTotalValue;
        @BindView(R.id.lblPersonaliseCheck)
        CheckedTextView lblPersonaliseCheck;
        @BindView(R.id.txtTo)
        EditText txtTo;
        @BindView(R.id.txtFrom)
        EditText txtFrom;
        @BindView(R.id.txtMessage)
        EditText txtMessage;
        @BindView(R.id.personaliseLayout)
        ConstraintLayout personaliseLayout;
        @BindView(R.id.spinnerAmount)
        Spinner spinnerAmount;
        @BindView(R.id.imgCloseForm)
        ImageView imgCloseForm;
        View view = null;


        public ViewHolder(View view) {
            super(view);

            ButterKnife.bind(this, view);
            lblTotalValue.setVisibility(View.VISIBLE);
            //remove icon is choosen to delete the list
            imgCloseForm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("remove pos", getAbsoluteAdapterPosition() + " " + getBindingAdapterPosition());
                    removeFor(getBindingAdapterPosition());

                }
            });

            txtRecMailAddress.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    giftVoucherList.get(getBindingAdapterPosition()).setGfReceiversMail(s.toString());


                }
            });


        }

    }


}
'''

This is the code for creating a new list 
''' public void createPersonalAdapter() {
        giftVoucherPersonalAdapter1 = new GiftVoucherPersonalAdapter(this.getContext(),
                giftVoucherList, this::getDetails, amountArray, amountValue, this::onAmountChange);
        if (giftVoucherPersonalAdapter1 != null) {

            if (giftVoucherList.size() >= 0) {
                GiftVoucherModel giftVoucherModel = new GiftVoucherModel();
                giftVoucherModel.setGfNo("");
                giftVoucherModel.setGfTotal("");
                giftVoucherModel.setGfSpinnerValue("");
                giftVoucherModel.setGfReceiversMail("");

                giftVoucherList.add(giftVoucherList.size(), giftVoucherModel);
                giftVoucherPersonalAdapter1.notifyItemInserted(giftVoucherList.size() - 1);
                Toast.makeText(getContext(), String.valueOf(giftVoucherList.size()), Toast.LENGTH_LONG).show();
            }

            personalRecyclerView.setAdapter(giftVoucherPersonalAdapter1);
            personalRecyclerView.invalidate();
        }
    }

'''

how to display overall total and also add new list without affecting the old list values.

0 Answers
Related