androidlistviewandroid-edittextandroid-adapterconvertview

Text values has order changes when scroll the listview in android


When I click to edit my EditText in my list-view, the order of items change. I think the problem is on adapter, but I don't know how to fix it.

Ex.: The last item goes to second position.

Adapter:

public class adapterAvaliacao extends BaseAdapter {

private LayoutInflater mInflater;
private List<Nota> itens;
DecimalFormat formatoNota = new DecimalFormat("#0.00");

public adapterAvaliacao(Context context, List<Nota> itens) {
    this.itens = itens;
    mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return itens.size();
}

@Override
public Nota getItem(int position) {
    return itens.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressLint({ "ViewHolder", "InflateParams" }) @Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        Nota item = itens.get(position);

        convertView = mInflater.inflate(R.layout.avaliacao_adapter, null);
        ((TextView) convertView.findViewById(R.id.lbl_nomeAtividade)).setText(item.getTitulo());
        if (item.isLancada()) {
            ((EditText) convertView.findViewById(R.id.txt_notaPostada)).setText(String.valueOf(formatoNota.format(item.getNota() * 10 / item.getPeso())).replace(",", "."));
        }
    } else {
        convertView.setTag(convertView.getTag());
        }
    return convertView;
}

Solution

  • Yes, it's the normal behaviour of the listView, therefore store your values in arraylist or model as you like so that you can set that whenever scroll.

    Because of recycling of listview items, you have to do that.

    your line of code to setText should be written after else part:

    if (convertView == null) {
    //your stuffs like find edit text
    
        }
        else {
                convertView.setTag(convertView.getTag());
             }
        editText.setText("hello world");