Problems with EditText in a listview

4

I'm trying to get my listview to hold the values placed in EditText , but every time I scroll I lose the data or it multiplies to other fields. Can anyone help? Below is my adapter :

public class WorkoutAdapter extends ArrayAdapter<Casa> {
protected Context context;
protected LinkedList<Casa> casas;

public WorkoutAdapter(Context context, LinkedList<Casa> casas){
    super(context, R.layout.exercise_layout, casas);
    this.context = context;
    this.casas = casas;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent){
    Vholder holder;
    if (convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.casa_layout, null);
        holder = new Vholder();
        holder.repts = (EditText)convertView.findViewById(R.id.etRepts);
        convertView.setTag(holder);
    }else{
        holder = (Vholder)convertView.getTag();
    }
    Casa current = casas.get(position);

    holder.repts.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) {
            current.setRepts(Integer.parseInt(s.toString()));
        }
    });
    holder.repts.setText(current.getRepts() + "");
            return convertView;
}
static class Vholder{
    EditText repts;
}

}

    
asked by anonymous 08.07.2015 / 02:06

1 answer

2

The problem is that whenever EditText is reused, by using holder , a new TextWatcher is added to it.

The solution goes through before adding a new TextWatcher remove the previous one.

public class WorkoutAdapter extends ArrayAdapter<Casa> {

    protected Context context;
    protected LinkedList<Casa> casas;

    public WorkoutAdapter(Context context, LinkedList<Casa> casas){
        super(context, R.layout.exercise_layout, casas);
        this.context = context;
        this.casas = casas;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        Vholder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.casa_layout, null);
            holder = new Vholder();
            holder.repts = (EditText)convertView.findViewById(R.id.etRepts);
            holder.textWatcher = null; 
            convertView.setTag(holder);
        }else{
            holder = (Vholder)convertView.getTag();
        }
        Casa current = casas.get(position);

        //Criar o TextWatcher 
        TextWatcher textWatcher = 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) {
                current.setRepts(Integer.parseInt(s.toString()));
            }
        });

        //Remover o TextWatcher anterior
        if(holder.textWatcher != null){
            holder.repts.removeTextChangedListener(holder.textWatcher)
        }

        //Adicionar o TextWatcher novo
        holder.repts.addTextChangedListener(textWatcher);
        holder.textWatcher = textWatcher;

        holder.repts.setText(current.getRepts() + "");
        return convertView;
    }
    static class Vholder{
        EditText repts;
        //Guarda a referência ao TextWatcher associado a este EditText
        TextWatcher textWatcher;
    }

}
    
08.07.2015 / 11:04