Problem deleting entry from Recycler View

This issue is driving me crazy. I’ve tried 20 different things by now. So let’s see if someone here can help me.

I am making a ToDo list APP using RecyclerView that stores the data in Firebase.

The app has a TasksActivity where all the tasks appear in the RecyclerView. I have a button that goes to a Task Creation dialogue. And I can create tasks, they appear then in the RecyclerView (Tasks Activity) and are updated in the firebase with no issue. I can close the APP and come back later and everything works well, all the entries appear in the app again when I load it. I can also swipe to delete an entry, and the entry also gets eliminated from the Database in firebase.

The problem, is when I create a TASK and without closing the app, I try to delete the task that I just created. It doesn’t allow me to do that. When I create a new task, and instantly delete it before closing the app, it appears again. But If I then close the app, and load it again, that entry can be deleted normally, but if I am in the same session in which I created the entry there’s no way I can delete it.

I was using some Log.d arguments to see how it changes. I think for various reasons that the problem is OnDataChange(). But so far I haven’t been able to come to the root of the issue. This is the TaskActivity class, and after this I will paste the TasksCreation (I don’t think pasting the Adapter is necessary).

public class TasksActivity extends AppCompatActivity  {

    DatabaseReference reference;
    RecyclerView myTasks;
    ArrayList<TaskItems> myTasksList;
    TasksAdapter tasksAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tasks);
        myTasks = findViewById(R.id.my_tasks);   // RecyclerView that I defined as part of the layout. This is the id of it

        myTasks.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        myTasksList = new ArrayList<>();
        Button openCreateTask = findViewById(R.id.openCreateTask);
        tasksAdapter = new TasksAdapter(this,myTasksList); 
        myTasks.setAdapter(tasksAdapter);
        new ItemTouchHelper(itemTouchHelper).attachToRecyclerView(myTasks);


        openCreateTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_task = new Intent(getApplicationContext(), TasksCreation.class);
                startActivity(intent_task);
            }
        });


        reference = FirebaseDatabase.getInstance().getReference().child("MotApp"); // Name of the App in the database .child("MotApp")

        reference.addValueEventListener(new ValueEventListener() {


            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {   // It gets the info from the database
                Log.d("data Changed called", "onDataChange: is called");
                Log.d("whatever", "onDataChange BEGIN Array of myTasksList size is "+myTasksList.size());
                myTasksList.clear();                                            // Added later to avoid duplication




                for(DataSnapshot elements: dataSnapshot.getChildren()){


                    TaskItems p = elements.getValue(TaskItems.class);
                    myTasksList.add(p);

                }


                tasksAdapter.notifyDataSetChanged(); // If this is put outside of onDataChange, it displays a blank list.
                Log.d("whatever", "onDataChange END Array of myTasksList size is "+myTasksList.size());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_SHORT).show();
            }


        });

    }


    ItemTouchHelper.SimpleCallback itemTouchHelper = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            Log.d("ARRAY SIZE", "onSwiped BEGIN Array of myTasksList size is "+myTasksList.size());
            String key =    myTasksList.get(position).getKey();
            reference= FirebaseDatabase.getInstance().getReference().child("MotApp").child(key);


        Toast.makeText(getApplicationContext(),"This is key "+key,Toast.LENGTH_LONG).show();
         reference.removeValue();
        myTasksList.remove(position);
        tasksAdapter.notifyItemRemoved(position);

            Log.d("ARRAY SIZE", "onSwiped END Array of myTasksList size is "+myTasksList.size());

        }
    };

}

And this is for TasksCreation Activity:

public class TasksCreation extends AppCompatActivity {

    DatabaseReference referenceCreation;
    ArrayList<String> list;
    EditText taskName;
    EditText taskDescr;
    Button selectDates;
    TextView taskDate;
    Button createTask;
    Button cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tasks_creation);

        taskName = findViewById(R.id.et_TaskName);
        taskDescr = findViewById(R.id.et_TaskDescr);
        selectDates = findViewById(R.id.selectDates);
        taskDate= findViewById(R.id.tv_Dates);
        createTask = findViewById(R.id.createTask);
        cancel = findViewById(R.id.cancelButton);


        createTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(taskName.getText()) && !TextUtils.isEmpty(taskDate.getText())) {

                    referenceCreation = FirebaseDatabase.getInstance().getReference().child("MotApp").push(); //saves it with custom key created by Firebase
                    final String key = referenceCreation.getKey();

                    referenceCreation.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {       
                            dataSnapshot.getRef().child("key").setValue(key);

                            dataSnapshot.getRef().child("taskTitle").setValue(taskName.getText().toString());
                            dataSnapshot.getRef().child("taskDescription").setValue(taskDescr.getText().toString());
                            dataSnapshot.getRef().child("taskDate").setValue(taskDate.getText().toString());

                        }


                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });


                    Intent intent = new Intent(getApplicationContext(), TasksActivity.class);

                    startActivity(intent);

                } else{
                    Toast.makeText(getApplicationContext(), "You haven't filled all the fields", Toast.LENGTH_SHORT).show();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), TasksActivity.class);

                startActivity(intent);
            }
        });

        selectDates.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createAlertDialogue();
            }
        });

    }

    private void createAlertDialogue(){
        list = new ArrayList<String>();

        AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyDialogTheme);


        builder.setTitle("Select days");
        builder.setMultiChoiceItems(R.array.Days, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                String arr[] = getResources().getStringArray(R.array.Days);

                if(isChecked){
                    list.add(arr[which]);
                }else if(list.contains(arr[which])){
                    list.remove(arr[which]);

                }

            }
        });

         builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

            String data = "";
            @Override
            public void onClick(DialogInterface dialog, int which) {
            for(String elements: list){
                elements = elements.substring(0,3);
                data= elements+" "+data;

            }
                taskDate.setText(data);

            }
        });


        builder.create();

        builder.show();

    }

}

I really appreciate any light to the issue.

Thanks a lot

P:S: This is the code for the TaskAdapter (Ignore the dayoftheweek part is just for a part of the code that I commented )

public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.MyViewHolder> { // V 1.3 added OnClickListener


    Context context;
    ArrayList<TaskItems> tasks;
    DatabaseReference reference;
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Date d = new Date();
    final String dayOfTheWeek = sdf.format(d).substring(0,3);


    public TasksAdapter(Context context, ArrayList<TaskItems> tasks) {
        this.context = context;
        this.tasks = tasks;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {  // V1.3 Added implements View.OnClickListener

        TextView taskTitle;
        TextView taskDate;
        TextView taskDescription;
        CheckBox taskCheckBox;
        ConstraintLayout constraintLayout;  // Added to change background  of each RecyclerView item.



        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            taskTitle = itemView.findViewById(R.id.taskTitle);
            taskDate = itemView.findViewById(R.id.taskDate);
            taskDescription = itemView.findViewById(R.id.taskDescription);
            taskCheckBox = itemView.findViewById(R.id.taskCheckBox);
            constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.item_task_layout);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = getAdapterPosition();

                    //            if(pos != RecyclerView.NO_POSITION){//Checks if item still exists
                    TaskItems clickedDataItem = tasks.get(pos);
                    Toast.makeText(v.getContext(), "You clicked " + clickedDataItem.getTaskTitle(), Toast.LENGTH_SHORT).show();
                    //          }
                }
            });

        }

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {                  // standard code for onCreateViewHolder

        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_task,parent,false));
    }



    @Override
    public void onBindViewHolder(final @NonNull MyViewHolder holder, final int position) {  // This method is called once for each item on the list.

            holder.taskTitle.setText(tasks.get(position).getTaskTitle());
            holder.taskDescription.setText(tasks.get(position).getTaskDescription());
            holder.taskDate.setText(tasks.get(position).getTaskDate());
            holder.taskCheckBox.setChecked(tasks.get(position).isChecked());
            holder.taskCheckBox.setTag(tasks.get(position).getKey());


            holder.taskCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    FirebaseDatabase database = FirebaseDatabase.getInstance();


                    String post = (String) holder.taskCheckBox.getTag();
                    Toast.makeText(context,"This is checkbox number: "+post,Toast.LENGTH_SHORT).show();  // Working Well.        //change for new logic

                    reference =  database.getReference("MotApp").child(post);

                    boolean checkboxStatus =  holder.taskCheckBox.isChecked();
                    Log.d("Checked", "onClick: The taskcheckbox checked is "+holder.taskCheckBox.isChecked());

                    TaskItems value = new TaskItems(tasks.get(position).getKey(),tasks.get(position).getTaskTitle(), tasks.get(position).getTaskDate(),tasks.get(position).getTaskDescription(),checkboxStatus);
                    reference.setValue(value);
                   Toast.makeText(context,"This is a checkbox belonging to item "+tasks.get(position).getTaskTitle(),Toast.LENGTH_LONG).show();

                }
            });

    }

    @Override
    public int getItemCount() { //tells the adapter the size . if it's zero then it won't create anything
        return tasks.size();
    }

}

Answer

I finally found how to solve this.

First I changed all addValueEventListener with addListenerforSingleValueEvent in TaskActivity and TaskCreation. With addValueEventListener I realized that it was making several loops everytime a new task was created which caused all kind of problems.

And I also changed the way the startIntent was set up in TaskCreation. I had set it up initially after the Listener block. But the asynchronous nature of onDataChange was making that everytime I loaded the intent the updated information wasn’t set up with the new task that was just added.

By putting it inside the onDataChange it was solved. I have made several tests and now it’s working flawlessly.

I wasted many days trying to solve this. But I learned a lot of lessons in the process. So I guess they weren’t wasted days 🙂 .

Thanks to all who chipped in to help me with this issue.