D
D
Danil Ochagov2018-08-14 10:36:22
Java
Danil Ochagov, 2018-08-14 10:36:22

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)

----------------------------------------------
MainActivity
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();
            }
        });

--------------------------------------
5b7286a15d423581002941.png5b7286a637df3503765194.png5b7286a9d8840846831119.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-08-14
@danilochagov

for (int i = 0; i < listCheckBoxChecked.size(); i++) {
            if (listCheckBoxChecked.get(i)) {
                listTasks.remove(i);
                listDate.remove(i);
                listCheckBoxChecked.remove(i);
            }
        }

There is an error here. Always remove from the end. Otherwise, you're missing elements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question