0

Deleting items from FirebaseRecyclerAdapter was working fine till I shift to an MVP pattern. right now the adapter is not notifying the change and throwing an error. How can I solve this issue.

Here is my FirebaseRecyclerAdapter,

@Override
    public FirebaseRecyclerAdapter onEntryListChanged(Query query, FirebaseRecyclerOptions<EntryModel> options) {

        adapter = new FirebaseRecyclerAdapter<EntryModel, EntryHolder>(options) {
            @NonNull
            @Override
            public EntryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.entry_item_view,parent,false);


                return new EntryHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull final EntryHolder holder, final int position, @NonNull EntryModel model) {
                entryView.attachView(model.getTitle(),model.getContent(),model.getDate(),holder);

                holder.entryUpdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        updateFragment(adapter.getRef(position).getKey());
                    }
                });

                holder.entryDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("Entry").child(FirebaseAuth.getInstance().getUid());
                        mDatabase.child(adapter.getRef(position).getKey()).removeValue();
                        notifyDataSetChanged();

                    }
                });
                holder.expandArrow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean shouldExpand = holder.expandContent.getVisibility() == View.GONE;
                        ChangeBounds transition = new ChangeBounds();
                        transition.setDuration(200);

                        if(shouldExpand){
                            entryView.expandArror(holder);
                        }
                        else {
                            entryView.collapsArrow(holder);
                        }
                        entryView.startTransition(holder,transition,shouldExpand);



                    }
                });
            }
        };
        return adapter;

the problem is with the delete.

log file,

07-02 00:38:47.678 29773-29773/com.example.nejat.journalapptrail1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.nejat.journalapptrail1, PID: 29773
    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.firebase.ui.common.BaseObservableSnapshotArray.getSnapshot(BaseObservableSnapshotArray.java:70)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.getRef(FirebaseRecyclerAdapter.java:112)
        at com.example.nejat.journalapptrail1.EntryList.EntryListPresenter$1$2.onClick(EntryListPresenter.java:72)
        at android.view.View.performClick(View.java:6261)
        at android.view.View$PerformClick.run(View.java:23752)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Nejweti
  • 113
  • 2
  • 11

3 Answers3

0

you are getting

 java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

you are trying to access Index 1, but you array size is 1, you need access to 0 Index.

try to decrease in 1 your position value for access to correct field in array.

mDatabase.child(adapter.getRef(position-1).getKey()).removeValue();
Bonestack
  • 81
  • 6
0

There are a few simple mistakes to fix:

  • Anywhere you use position, change it to holder.getAdapterPosition(). (To make catching those mistakes easier, remove the final modifier from your position parameter.)
  • Remove notifyDataSetChanged() calls, this is done for you.
SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61
0

It seems you are making mistake in getting the values from the child nodes.

I have fond a sample implementation for FirebaseRecyclerAdapter.

https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseRecyclerAdapter.java

I have also tried this and it worked for me.

Akash Sahu
  • 171
  • 1
  • 9