I have a RecyclerView with an EditText in each row. Each EditText has a TextWatcher attached to it and an object in an array to store information about the row and the value of the EditText.
The problem I'm facing is that when the row goes offscreen, it gets recycled, but in that process, onTextChanged is called with an empty string, meaning an empty string is being saved to the row's information object. When that row is loaded again, it just loads a blank value, effectively deleting whatever content was in the EditText before. This seems like very useless functionality, because (as far as I can tell) there's no way to differentiate between a recycle clear and the user actually clearing the value in the EditText, making EditTexts in a RecyclerView completely useless.
my onBindViewHolder
method is this:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int value = indicators.get(position).getValue();
System.out.println("VALUE: " + indicator.getValue());
if (value == -1) {
viewHolder.inputBox.setText("");
} else {
viewHolder.inputBox.setText(Integer.toString(value));
}
viewHolder.editListener.updatePosition(position);
}
My TextWatcher is this:
private class EditBoxListener implements TextWatcher {
private int position;
private CharSequence sequence;
private boolean initialCall = true;
public void updatePosition(int _position) {
position = _position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
System.out.println("TEXT CHANGED TO: " + charSequence.toString());
if (!initialCall) {
int value = -1;
try {
value = Integer.parseInt(charSequence.toString());
} catch (Exception e) {}
if (indicators.size() > 0) {
indicators.get(position).setValue(value);
}
} else {
initialCall = false;
}
}
@Override
public void afterTextChanged(Editable editable) {}
}