滚动时正在重置动态构建的复选框

时间:2018-06-11 15:14:59

标签: java android realm android-adapter

我有一个带适配器的列表视图,我创建了一些复选框,在适配器中我添加了一个on change listener来更改我的realm数据库中的值。就在这之上,我得到了存储在数据库中的当前值,以保留以前的值。

这看起来效果相当不错,但是当您滚动时,您检查过的项目会在经过屏幕时取消选中。

如下所示:

enter image description here enter image description here

生成此代码的代码:

Adaptor.java

if(check != null){
    viewHolder.checkbox.setText(check.title);

    if (check.completed == 1) {
        viewHolder.checkbox.setChecked(true);
    } else {
        viewHolder.checkbox.setChecked(false);
    }

    viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {
                check.setCompleted();
            } else {
                check.setNotCompleted();
            }
        }
    });
}

Fragment.java

final ChecksAdaptor checksAdaptor = new ChecksAdaptor(device.checks(), getContext());

listView.setAdapter(checksAdaptor);

完整适配器

public class ChecksAdaptor extends ArrayAdapter<DeviceCheck> {

    public RealmResults<DeviceCheck> checks;

    Context mContext;

    // View lookup cache
    private static class ViewHolder {
        CheckBox checkbox;
    }

    public ChecksAdaptor(RealmResults<DeviceCheck> checks, Context context) {
        super(context, R.layout.check_row_item, checks);
        this.checks = checks;
        this.mContext=context;
    }

    private int lastPosition = -1;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        final DeviceCheck check = getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag

        final View result;

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.check_row_item, parent, false);
            viewHolder.checkbox = convertView.findViewById(R.id.checkbox);

            result=convertView;

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }

        lastPosition = position;

        if(check != null){
            viewHolder.checkbox.setText(check.title);

            if (check.completed == 1) {
                viewHolder.checkbox.setChecked(true);
            } else {
                viewHolder.checkbox.setChecked(false);
            }

            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (isChecked) {
                        check.setCompleted();
                    } else {
                        check.setNotCompleted();
                    }
                }
            });
        }

        // Return the completed view to render on screen
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:1)

根据mTak

的建议
viewHolder.checkbox.setOnCheckedChangeListener(null)