0

I have a TextView on each item and added holder.item_edit_text.addTextChangedListener on onBindViewHolder for a long list and an interface that computes the total at the fragment level but weirdly when a user types on one view, onBindViewHolder is called on multiple items. I.E if a user enters 5, i get multiples of 5 depending on the recycling (number of items in the list) e.g 15 or 25

RECYCLERVIEW onBindViewHolder

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

        }

        @Override
        public void onTextChanged(CharSequence amount, int start, int before, int count) {
            calculateTotal.onAmountAdded(current, amount.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    @Override
      public int getItemCount() {
        if (Accounts != null) {
        return Accounts.size();
    }
    return 0;
   }

Question is how I can ensure that onBindViewHolder is called for one item in the recyclerview per time

THE FRAGMENT

     @Override
public void onAmountAdded(Account account, String amount) {
private Map<Account, String> orderItems = new HashMap<>();        
    if (amount.length() < 9) {
        orderItems.put(account, amount);
        int newTotal = 0;
        Iterator iterator = orderItems.entrySet().iterator();
        while (iterator.hasNext()) {
            try {
                Map.Entry mentry = (Map.Entry) iterator.next();
                newTotal += Integer.parseInt(mentry.getValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        text_total.setText(ValidationUtil.formatNumber(String.valueOf(newTotal)));
        return;
    }
}
clifford_owino
  • 462
  • 1
  • 6
  • 24

1 Answers1

1

Not enough information I suppose.

Instead of adding a TextWatcher on viewBinder

add it inside your ViewHolder Class

class ViewHolder extends RecyclerView.ViewHolder {

    private TextView item_edit_text;

    ViewHolder(View itemView) {
        super(itemView);
        item_edit_text = itemView.findViewById(R.id.item_edit_text);
        item_edit_text.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence amount, int start, int before, int count) {
                   //not sure why this current variable is for
                  //if its current Account 
                   Accounts current = Accounts.get(getLayoutPosition());
                   calculateTotal.onAmountAdded(current, amount.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }
shb
  • 5,957
  • 2
  • 15
  • 32
  • the issue is at `ViewHolder` level, I don't get access to the current object. That's the value of using `onBindViewHolder`. To elaborate, if I have a list of 20 items and my screen can only populate 5 items at a time, that means that the items are going to be recycled 4 times... so in that case, if a user enters 5, i get 20 instead.. (5*4). Is tyhat descriptive enough? – clifford_owino Oct 19 '18 at 21:00
  • @clifford_owino You can have access to the current object of which the data is being changed. 'current' is an object of Account right? how are you getting current object at onBindViewHolder something like 'Account current = Accounts.get(position);' you can do the same on ViewHolder just use getLayoutPosition() to get the current object from 'Accounts' array. – shb Oct 20 '18 at 19:11
  • This one worked https://stackoverflow.com/questions/31844373/saving-edittext-content-in-recyclerview – clifford_owino Oct 24 '18 at 11:36
  • Serves the same purpose in a different way. Code was inadequate, that's why I went with getLayoutPosition() instead of getAdapterPosition(). getAdapterPosition(). – shb Oct 24 '18 at 12:41