4

Estou tentando fazer com que minha listview retenha os valores colocados no EditText, mas toda vez que eu dou um scroll eu perco os dados ou ele se multiplica pra outros campos. Alguém pode ajudar? Segue abaixo meu 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 Resposta 1

2

O problema está em que, sempre que o EditText é reaproveitado, recorrendo-se ao holder, é-lhe adicionado um novo TextWatcher.

A solução passa por antes de adicionar um novo TextWatcher remover o anterior.

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

}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .