0

So I have a ListView for which I made a custom adapter using a layout with a TextView and an EditText.

For the EditText I use a List<String> to store the content of all the EditTexts in the list. I do this using a OnFocusChangeListener().

The problem I'm having is that when an EditText is focused and I scroll enough so it's view is recycled, I loose the content of it, but if I click away so it becomes unfocused before scrolling it out of visible area it works fine.

I was thinking of saving the content of it when it is recycled, or destroyed, so that is why I'm asking what method is called when it is scrolled out of visible area. Or I can do this when the list starts scrolling, if there is a method I can use for this.

Here is the getView method from my custom adapter:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if(convertView == null)
    {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_item_edit_content, null);

        holder.input = (EditText) convertView.findViewById(R.id.input);
        holder.title = (TextView) convertView.findViewById(R.id.rowName);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.title.setText(columnNames.get(position));
    holder.input.setText(values.get(position));

    holder.input.setOnFocusChangeListener(new OnFocusChangeListener() { 
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus)
            {
                values.set(position, holder.input.getText().toString());
            }
        }
    });
    return convertView;
}

private class ViewHolder
{
    EditText input;
    TextView title;
}
laurGanta
  • 175
  • 1
  • 1
  • 12
  • please check the recycle mechanism of ListView here http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works. This might help you for your approach. – Vaibs Dec 16 '14 at 10:01
  • @Vaibs. Please tell exactly what has to be done. I think the info there is useless to solve the problem here. – greenapps Dec 16 '14 at 10:07
  • I already read that a while ago. I know how it works. I figured out a fix for this with a different aproach. I'll post an update when it's done – laurGanta Dec 16 '14 at 10:18
  • Is values your List? If so, the problem is it only gets updated when the `EditText` loses focus. A way around this might be to use a `TextWatcher` to check whenever the user types in the `EditText` and update the relevant string in the `List` each time. I don't know of a method which is called when a list item scrolls off the screen but the `getView(...)` method could be used to achieve what you want. If `convertView` is NOT null then it's an existing list item possibly with text in the `EditText` - get the text and add to the `List` before creating the new list item. – Squonk Dec 16 '14 at 10:18
  • @Squonk http://stackoverflow.com/questions/27490829/why-edittext-within-listview-using-textwatcher-looses-the-content-in-it-after-sc this is my first attempt at doing this. I'm currently considering using `onScrollListener` for the `ListView` this adapter is attached to. So everytime I start scrolling it saves the data in the view that is focused (`getCurrentFocus()`), but I'm not sure how to identify that `View`'s position in my `ListView`. – laurGanta Dec 16 '14 at 10:37

1 Answers1

0

You could use an other listener:

holder.input.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 
Groco
  • 1,301
  • 15
  • 19
  • http://stackoverflow.com/questions/27490829/why-edittext-within-listview-using-textwatcher-looses-the-content-in-it-after-sc. It got really messy so i changed to `onFocusChangeListener()` – laurGanta Dec 16 '14 at 10:39
  • It got really messy? I don't see why. Could you explain to me? – Groco Dec 16 '14 at 10:45