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. IE if a user enters 5, i get multiples of 5 depending on the recycling (number of items in the list) eg 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;
}
}
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) {
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.