Edittext issues in a listview

Asked

Viewed 271 times

4

I’m trying to get my listview retain the values placed on EditText, but every time I give one scroll I lose the data or it multiplies to other fields. Can anyone help? Follow below mine 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;
}

}

1 answer

2

The problem is that whenever the Edittext is reused using the holder, you are added a new Textwatcher.

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

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;
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.