Android app crashes upon removing an item from FirebaseRecyclerAdapter

2hTu2

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);
Boonya Kitpitak

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Removing Last Item From Android ListView Crashes The App

From Dev

Removing last item from ListView crashes the app

From Dev

BluetoothChat app crashes upon onDestroy() in Android 4.4

From Dev

Android App crashes upon menu button click

From Dev

Android App crashes upon menu button click

From Dev

Updating the database item from FirebaseRecyclerAdapter android

From Dev

App Crashes upon receiving data from intent

From Dev

App Crashes upon receiving data from intent

From Dev

Android ListView: Removing Sub item dependent upon the item

From Dev

App crashes upon launch

From Dev

Android removing an item from a gridview

From Dev

NSInvalidArgumentException: App crashes upon opening

From Dev

NSInvalidArgumentException: App crashes upon opening

From Dev

Deleting item in tableView by removing item from array crashes with fatal error: Index out of range

From Dev

Github App for Mac crashes upon opening

From Dev

UrbanAirship app crashes upon receipt of message

From Dev

My App crashes upon creating Sqlite DB

From Dev

Crop image from gallery crashes app - Android

From Dev

Android app crashes opening InputStream from an XML

From Dev

Removing AdMob from app in Android Studio

From Dev

Removing an item from RecyclerView

From Dev

Removing an item from a JList

From Dev

Not removing item from the cart

From Dev

Removing an item from a list

From Dev

Removing an item from a stack, whilst iterating over it, in android

From Dev

toast after removing last item from recyclerview android

From Dev

App crashes on spinner item click

From Dev

Retrieve data from onbindviewholder using FirebaseRecyclerAdapter android

From Dev

Android app from bootcamp book crashes when written with Android Studio

Related Related

  1. 1

    Removing Last Item From Android ListView Crashes The App

  2. 2

    Removing last item from ListView crashes the app

  3. 3

    BluetoothChat app crashes upon onDestroy() in Android 4.4

  4. 4

    Android App crashes upon menu button click

  5. 5

    Android App crashes upon menu button click

  6. 6

    Updating the database item from FirebaseRecyclerAdapter android

  7. 7

    App Crashes upon receiving data from intent

  8. 8

    App Crashes upon receiving data from intent

  9. 9

    Android ListView: Removing Sub item dependent upon the item

  10. 10

    App crashes upon launch

  11. 11

    Android removing an item from a gridview

  12. 12

    NSInvalidArgumentException: App crashes upon opening

  13. 13

    NSInvalidArgumentException: App crashes upon opening

  14. 14

    Deleting item in tableView by removing item from array crashes with fatal error: Index out of range

  15. 15

    Github App for Mac crashes upon opening

  16. 16

    UrbanAirship app crashes upon receipt of message

  17. 17

    My App crashes upon creating Sqlite DB

  18. 18

    Crop image from gallery crashes app - Android

  19. 19

    Android app crashes opening InputStream from an XML

  20. 20

    Removing AdMob from app in Android Studio

  21. 21

    Removing an item from RecyclerView

  22. 22

    Removing an item from a JList

  23. 23

    Not removing item from the cart

  24. 24

    Removing an item from a list

  25. 25

    Removing an item from a stack, whilst iterating over it, in android

  26. 26

    toast after removing last item from recyclerview android

  27. 27

    App crashes on spinner item click

  28. 28

    Retrieve data from onbindviewholder using FirebaseRecyclerAdapter android

  29. 29

    Android app from bootcamp book crashes when written with Android Studio

HotTag

Archive