In an activity I have a CustomListAdapter
that contains two EditText
fields for each row in the CustomListAdapter
. Currently, I have OnFocusChangeListener
s that updates the values taken from the EditText
, and saves them into a database. However, the last changed value never gets updated. All changed values are saved, but the last one doesn't get saved because the focus was never changed after it was modified. The user changes the last value, clicks "back", and the OnFocusChangeListener
never gets called again; therefore, the last value doesn't get saved.
So I tried to use an AddOnTextChangedListener
... however, I can only update the first element in the CustomListAdapter
. Not only that, whenever I update the first element, it propagates to all the rest of the elements in the CustomListAdapter
, setting the values of the EditText
of all other rows to the first EditText
field's value. Whenever I change any other row, nothing happens. I saw that the TextChangeListener
gets called for every position in the list adapter, even if the text was not changed.
Is there a way to add a listener to each EditText
in the CustomListAdapter
so that it updates each individual EditText
element corresponding to the correct data[position]
in the CustomListAdapter
?
Code snippet:
public class CustomListAdapter extends ArrayAdapter {
ArrayList data;
[...]
class MyTextWatcher implements TextWatcher {
private int position;
public MyTextWatcher(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
arraymap.put(position, Double.valueOf(s.toString()));
}
}
[...]
public View getView(final int position, View convertView, ViewGroup parent) {
final EditText editText1 = (EditText) convertView.findViewById(R.id.edit_text1);
final EditText editText2 = (EditText) convertView.findViewById(R.id.edit_text2);
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
data[position].save();
}
}
}
});
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
data[position].save();
}
}
}
});
[...]
//----------------------ALSO TRIED----------------------------
editText1.addTextChangedListener(new MyTextWatcher(position));
[...]
}
}