Checkedtextview check/uncheck after scroll Listvie

2020-04-26 04:40发布

问题:

I'm developing checkedtextview in listvew using viewHolder and getview. To populate check/uncheck status binding from database is running well. But, if I check item and then scroll listview, it will back to uncheck.

here is my customAdapter code.

public class CustomAdapter extends ArrayAdapter<Model> {
private Activity activity;
int id;
String autoid;
ArrayList<Model> items;
DatabaseHelper dbhelper = new DatabaseHelper(getContext());

public CustomAdapter(Activity context, int resource, ArrayList<Model> obj) {
    super(context, resource, obj);

    this.activity = context;
    this.id = resource;
    this.items = obj;

    try {
        dbhelper.openDatabase();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}



static class ViewHolder {
    protected CheckedTextView textView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder vh = null;

    if (convertView == null) {

        LayoutInflater inflater = activity.getLayoutInflater();
        convertView = inflater.inflate(id, null);
        vh = new ViewHolder();
        convertView.setTag(vh);

        vh.textView = (CheckedTextView) convertView.findViewById(R.id.textbox1);

    }
    else
    {
        vh = (ViewHolder) convertView.getTag();

    }

    final Model item = items.get(position);
    String status = item.getStatus();

    final Model check = getItem(position);

    final ViewHolder finalVh = vh;

    vh.textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            autoid = item.getAutoid();

            if (! finalVh.textView.isChecked()) {
                finalVh.textView.setChecked(true);
            }
            else
            {
                finalVh.textView.setChecked(false);
            }
        }
    });

    vh.textView.setText(item.getDate() +". "+item.getInfo());
    vh.textView.setChecked(Boolean.parseBoolean(status));

    return convertView;
}}

Thanks in advance.

回答1:

Thats because your list(items) which you passed has the old value.When you change the check status,update it to the list(items) which is populated to the listview.On Scroll the list view will load them items again, where it is getting the old value.

vh.textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        autoid = item.getAutoid();

        if (! finalVh.textView.isChecked()) {
            finalVh.textView.setChecked(true);
            item.setStatus("true")
        } else {
            finalVh.textView.setChecked(false);
            item.setStatus("false");
        }
        items.set(position, item);
    }
});


回答2:

This is because your ViewHolder is reused, in a ListView you do not have the same instance number ViewHolder and element in the list, You have approximately the number of item displayed.

To fix it you can store if an item was checked in boolean list (same size of your item list) and in your getView set your item to checked if the boolean at the same position is true.