ListView with checkbox and custom adapter, fragment not working properly

Viewed 57

I want to make an listview with checkboxes. But the checkbox check is not working properly, is checkin right just the first element in my list. Can you help me pls? Here is my code

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

} How can i gen the right position, i mean the right item to check/uncheck

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

1 Answers

With the @blackapps's help, try this:

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"));
            }
        }
    });
Related