FirebaseRecyclerAdapter在删除数据时不更新项目索引

时间:2016-07-24 21:45:52

标签: android firebase android-recyclerview firebase-realtime-database firebaseui

我有一个项目列表,每个项目都有一个名称和一个删除按钮。

我的 populateViewHolder 方法

  @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);
     }
   });
 }

我的 ViewHolder :在我的ViewHolder中,我有一个 bindToSubChannel 方法,该方法获取FirebaseRecyclerAdapter&amp;在删除ImageView上设置clickListener。

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);
  }
}

我在此行获得以下 IndexOutOfBoundsException

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

当我在列表中有6个项目(1,2,3,4,5,6)&amp;我删了1,2。

然后,当我按下删除6时,适配器为项目6检索的索引是原始6但是适配器的大小已更改为4(删除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)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

而不是在绑定position时将ViewHolder视为最终,而是创建对该键的最终引用:

  @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);
     }
   });
 }