带有复选框和自定义适配器的 ListView,片段无法正常工作

ListView with checkbox and custom adapter, fragment not working properly

我想制作一个带有复选框的列表视图。但是复选框检查无法正常工作,是否正确检查了我列表中的第一个元素。你能帮我吗?这是我的代码

public class ToDoAdapter extends ArrayAdapter<ToDo> {

private FirebaseAuth mAuth;
FirebaseUser user;
FirebaseFirestore db;

private Context context;
private int resource;
private List<ToDo> list;
private LayoutInflater inflater;
CheckBox checkBox;
ToDo toDo;

public ToDoAdapter(@NonNull Context context, int resource, @NonNull List<ToDo> objects,
                   LayoutInflater inflater) {
    super(context, resource, objects);
    this.context=context;
    this.resource=resource;
    this.list=objects;
    this.inflater=inflater;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View view = inflater.inflate(resource,parent, false);
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    db = FirebaseFirestore.getInstance();
    view.setClickable(true);
    view.setFocusable(true);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("todoID",position);
            bundle.putSerializable("item", ToDoFragment.arrayAdapterToDo.getItem(position)
                    .getDetalii());
            bundle.putSerializable("deadline", ToDoFragment.arrayAdapterToDo.getItem(position)
                    .getDeadline().toString());
            ToDoFragment.getInstance().newFragment(bundle);
        }
    });
    toDo = list.get(position);
    if(toDo!=null){
        addData(view, toDo.getDeadline());
        addDetalii(view, toDo.getDetalii());
        check(view, toDo.isEsteEfectuat());

    }

    checkBox = view.findViewById(R.id.checkbox);

    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(checkBox.isChecked()) {
*****
                checkBox.setChecked(true);
                toDo.setEsteEfectuat(true);
                updateCheckBox();
                view.setBackgroundColor(Color.parseColor("#008000"));
            } else {
                checkBox.setChecked(false);
                toDo.setEsteEfectuat(false);
                updateCheckBox();
                view.setBackgroundColor(Color.parseColor("#FF0000"));
            }
        }
    });

    return view;
}

} 我怎样才能生成正确的位置,我的意思是 check/uncheck

的正确项目

If i put a toast instead of ****, i gen the first item of the list

在@blackapps 的帮助下,试试这个:

CheckBox checkBox = view.findViewById(R.id.checkbox);
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toDo = ToDoFragment.arrayAdapterToDo.getItem(position);
            if(checkBox.isChecked()) {
                checkBox.setChecked(true);
                toDo = ToDoFragment.arrayAdapterToDo.getItem(position);
                toDo.setEsteEfectuat(true);
                updateCheckBox();
                view.setBackgroundColor(Color.parseColor("#008000"));
            } else  {
                checkBox.setChecked(false);
                toDo = ToDoFragment.arrayAdapterToDo.getItem(position);
                toDo.setEsteEfectuat(false);
                updateCheckBox();
                view.setBackgroundColor(Color.parseColor("#FF0000"));
            }
        }
    });