0

I have a listview which contains an editext for each position. I have assigned a textwatcher for each edittext as below:

holder.prodQuantity.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) {
        if(isDialog) {
            recallMap.put(productId, s.toString());
        }
    }

The problem here is, whenever I add a value to the first edit text, the text watcher gets triggered for all items of the listview. recallMap should contain only the id of the selected row and the value entered in the edit text of that row, but in this case, recalMap has all the ids and the value entered in the first edit text for all. Please note that, this is happening without any scroll. Any help would be much appreciated. Thanks.

Vyshakh
  • 632
  • 1
  • 11
  • 29

2 Answers2

1

Before adding textChangedListner in adapter first remove already added textChangedListener to that EditText. Like

 holder.prodQuantity.removeTextChangedListener(textWatcher);
 // obviously you also need to maintain TextWatchers ArrayList associated with each EditText.
// Or instead of maintaining a separate ArrayList for TextWatcher, you can implement in your Data Model Class.

Then your code below

  holder.prodQuantity.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) {
    if(isDialog) {
        recallMap.put(productId, s.toString());
    }
}
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
0

Use this custom class

public class MyCustomEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
           //your edittext text is here
        }
    }

 @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
        return new ViewHolder(v, new MyCustomEditTextListener(), this);
    }

assign listener to edittext

edittext.addTextChangedListener(myCustomEditTextListener);

update list position

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.myCustomEditTextListener.updatePosition(position);
    }
BDC
  • 11
  • 5