I have been searching for an answer to this, but the solutions don't seem to work for me. I have a TextView and an EditText in a list item. I am trying to update the stored values for the EditTexts when the user edits them.
@Override
public View getView(int index, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final int pos = index;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.user_details_list_row, parent,false);
holder = new ViewHolder();
holder.mCaptionTextView = (TextView)convertView.findViewById(id.user_detail_row_caption);
holder.mDetailEditText = (EditText)convertView.findViewById(id.user_detail_row_value);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.mDetailEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
mUserDetails.set(pos, s.toString());
}
});
holder.mCaptionTextView.setText(mUserCaptions.get(index));
holder.mDetailEditText.setText(mUserDetails.get(index),BufferType.EDITABLE);
return convertView;
}
public static class ViewHolder{
public TextView mCaptionTextView;
public EditText mDetailEditText;
}
When I do this, scrolling triggers the TextWatcher and updates the values, overwriting correct text with duplicate text from one of the other EditTexts.
Instead of a TextWatcher, I've also tried this code:
holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
EditText et = (EditText)v.findViewById(id.user_detail_row_value);
mUserDetails.set(index, et.getText().toString().trim());
}
}
});
And it also updates the wrong EditTexts. What am I missing here?
Edit: Also tried this:
final ViewHolder testHolder = holder;
holder.mDetailEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
EditText et = (EditText)v.findViewById(id.user_detail_row_value);
mUserDetails.set(testHolder.ref, et.getText().toString().trim());
}
}
});
It corrects the scrolling change issue I was seeing, but now after editing one of the EditTexts, it changes a bunch of the others as well.