1

I created a remove function in the populateViewHolder like so:

viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        new AlertDialog.Builder(getContext())
                .setTitle("Are you sure?")
                .setMessage("Do you want to delete this note?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mNotesDatabase.child(list_id).removeValue();
                        notifyDataSetChanged();
                        notifyItemRemoved(position);
                    }
                })
                .setNegativeButton("No", null)
                .show();

        return true;
    }
});

This code is present in: public void onDataChange(DataSnapshot dataSnapshot). When I run my code and try to remove the item from the list, my app crashes. However, it does remove the item in the database itself. I think my app is not updating the FirebaseRecycleAdapter correctly. I tried using notifydatasetchanged(), which resulted in nothing. I also tried to override onChildRemoved. However onChildRemoved appears to not be a function inside FirebaseRecycleAdapter. I also tried to call the FirebaseRecycleAdapter itself, on which it told me it was not initialized.

Here is the logCat:

enter image description here

edit

Below this text you can find the code for the whole adapter as requested:

        final FirebaseRecyclerAdapter<Notes, NotesViewHolder> notesRecyclerViewAdapter = new FirebaseRecyclerAdapter<Notes, NotesViewHolder>(
            Notes.class,
            R.layout.feed_note_single_layout,
            NotesViewHolder.class,
            mNotesDatabase
    ) {

        @Override
        protected void populateViewHolder(final NotesViewHolder viewHolder, Notes model, final int position) {

            final String list_id = getRef(position).getKey();

            mNotesDatabase.child(list_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    final String title = dataSnapshot.child("title").getValue().toString();
                    final String message = dataSnapshot.child("message").getValue().toString();

                    viewHolder.setTitle(title);
                    viewHolder.setMessage(message);

                    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent noteIntent = new Intent(getContext(), NotesActivity.class);
                            noteIntent.putExtra("list_id", list_id);
                            noteIntent.putExtra("title", title);
                            noteIntent.putExtra("message", message);
                            startActivity(noteIntent);

                        }
                    });

                    viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            new AlertDialog.Builder(getContext())
                                    .setTitle("Are you sure?")
                                    .setMessage("Do you want to delete this note?")
                                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {



                                            mNotesDatabase.child(list_id).removeValue();
                                            notifyItemRemoved(viewHolder.getAdapterPosition());
                                            notifyDataSetChanged();


                                        }
                                    })
                                    .setNegativeButton("No", null)
                                    .show();

                            return true;
                        }
                    });

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    };

    mFeedList.setAdapter(notesRecyclerViewAdapter);
2hTu2
  • 378
  • 4
  • 19

2 Answers2

1

I think the problem might occur because when you delete the data in RecyclerView and call notifyDataSetChanged(). The position in populateViewHolder might refer to the deleted position.

Therefore, you should use viewHolder.getAdapterPosition() instead of position.

According to the log, the issue might also cause from onDataChange in NoteActivity please carefully check it.

Hope, it's help.

Boonya Kitpitak
  • 3,607
  • 1
  • 30
  • 30
1

call notifyDataSetChanged() after removing the item like below

 mNotesDatabase.child(list_id).removeValue();

 notifyItemRemoved(position);
 notifyDataSetChanged();
Jay Dwivedi
  • 454
  • 3
  • 15