Answer the question
In order to leave comments, you need to log in
Removes not all items from the TODO List of an android application?
I am making a TODO sheet and there was a problem when you click on the Select all button, then all the elements will be selected, but when you click Delete selected after that, 1 element remains. But if you have only 1 task in total, then everything will be deleted. What is the problem I don't understand?
fig1 before adding items, fig2 after adding, fig3 when I pressed the button to select all and the button to delete selected
checkForEmptyTask() only checks if (taskAdapter.getItemCount == 0) then displays the text that there are no tasks yet
---------- -------------------------------------------------- ------------------
TaskAdapter
public void selectAllElements () {
for (int i = 0; i < listCheckBoxChecked.size(); i++) {
listCheckBoxChecked.set(i, true);
}
notifyDataSetChanged();
}
public void removeSelected () {
for (int i = 0; i < listCheckBoxChecked.size(); i++) {
if (listCheckBoxChecked.get(i)) {
listTasks.remove(i);
listDate.remove(i);
listCheckBoxChecked.remove(i);
}
}
notifyDataSetChanged();
}
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
private List<String> listTasks = new ArrayList<>(); // хранит таски
private List<String> listDate = new ArrayList<>(); // хранит дату тасков
private List<Boolean> listCheckBoxChecked = new ArrayList<>(); // хранит значение чекбокса(по дефолту он false)
btn_sel_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
if (taskAdapter.getItemCount() == 0) {
showWrongDialog("No elements that you can select!");
} else {
taskAdapter.selectAllElements();
}
}
});
btn_del_sel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
taskAdapter.removeSelected();
checkForEmptyTasks();
showSuccessfullyDialog();
}
});
Answer the question
In order to leave comments, you need to log in
for (int i = 0; i < listCheckBoxChecked.size(); i++) {
if (listCheckBoxChecked.get(i)) {
listTasks.remove(i);
listDate.remove(i);
listCheckBoxChecked.remove(i);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question