Arnav Vohra
Arnav Vohra

Reputation: 397

FirebaseRecyclerAdapter not updating item indexes when data deleted

I have a list of items with each item having a name and a delete button.

My populateViewHolder method

  @Override
  protected void populateViewHolder(SubChannelViewHolder viewHolder, final SubChannel model, final int position) {

    //passing a clickListener to viewHolder,viewHolder in turn decides to which view it should assign this clickListener.
    viewHolder.bindToSubChannel(model, new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        String key= getRef(position).getKey();//Getting the key of the subChannel item

        Map<String,Object> childUpdates=new HashMap<>();

       //The positions where the subChannel was stored
       childUpdates.put("/channels-subChannels/"+mChannelKey+"/" +key+"/subChannelName/",null);
       childUpdates.put("/channels/" + mChannelKey+"/subChannels/"+key+"/subChannelName/", null);
       childUpdates.put("/user-channels/" +getUid()+"/"+ mChannelKey+"/subChannels/"+key+"/subChannelName/", null);

       //setting values at these location as null thereby doing batch deleting.
       mDatabase.updateChildren(childUpdates);
     }
   });
 }

My ViewHolder : inside my ViewHolder I have a bindToSubChannel method which takes the object retrieved by the FirebaseRecyclerAdapter & sets the clickListener on the delete ImageView.

public class SubChannelViewHolder extends RecyclerView.ViewHolder {

  private TextView subChannelNameTextView;
  private ImageView removeImageView;

  public SubChannelViewHolder(View itemView) {
    super(itemView);
    subChannelNameTextView=(TextView)itemView.findViewById(R.id.subChannel_name_text_view);
    removeImageView =(ImageView)itemView.findViewById(R.id.remove_image_view);
  }
  public void bindToSubChannel(SubChannel subChannel,View.OnClickListener RemoveImageClickListener){
    subChannelNameTextView.setText(subChannel.getSubChannelName());
    removeImageView.setOnClickListener(RemoveImageClickListener);
  }
}

I get the following IndexOutOfBoundsException at this line:

String key= getRef(position).getKey();

The Exception is thrown when say I had 6 items in the list(1,2,3,4,5,6) & I deleted 1,2.

Then when I press delete for 6 the index the adapter retrieves for item 6 is the original 6 but but the size of the adapter had changed to 4(after deletion of 1,2)

E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.arnavvohra.arry2, PID: 1493
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.firebase.ui.database.FirebaseArray.getItem(FirebaseArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getRef(FirebaseRecyclerAdapter.java:150)
at in.arnavvohra.arry2.EditSubChannelsActivity$2$1.onClick(EditSubChannelsActivity.java:67)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5930)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

How should I go about solving this issue?

Upvotes: 3

Views: 666

Answers (1)

Sam Stern
Sam Stern

Reputation: 25134

Rather than considering position to be final when binding the ViewHolder, instead create a final reference to the key:

  @Override
  protected void populateViewHolder(SubChannelViewHolder viewHolder, final SubChannel model, int position) {

    // Move this out here, mark it as final.  Then reference it inside the block.
    final String Key = getRef(position).getKey();

    //passing a clickListener to viewHolder,viewHolder in turn decides to which view it should assign this clickListener.
    viewHolder.bindToSubChannel(model, new View.OnClickListener() {
      @Override
      public void onClick(View view) {
       Map<String,Object> childUpdates=new HashMap<>();

       //The positions where the subChannel was stored
       childUpdates.put("/channels-subChannels/"+mChannelKey+"/" +key+"/subChannelName/",null);
       childUpdates.put("/channels/" + mChannelKey+"/subChannels/"+key+"/subChannelName/", null);
       childUpdates.put("/user-channels/" +getUid()+"/"+ mChannelKey+"/subChannels/"+key+"/subChannelName/", null);

       //setting values at these location as null thereby doing batch deleting.
       mDatabase.updateChildren(childUpdates);
     }
   });
 }

Upvotes: 5

Related Questions