【问题标题】:FirebaseRecyclerAdapter not updating item indexes when data deleted删除数据时,FirebaseRecyclerAdapter 不更新项目索引
【发布时间】:2016-11-28 04:04:07
【问题描述】:

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

我的 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 检索到的对象并在删除 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) 并且我删除了 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)

我应该如何解决这个问题?

【问题讨论】:

  • 您不必在适配器上调用notifyItemRemoved() 或类似名称。只需从数据库中删除项目,其余的会自动发生。
  • 最初我删除了它们,但搜索所以我发现应该添加它们。无论如何,即使我删除它们,我的错误仍然存​​在。
  • @FrankvanPuffelen 您或您团队中的某个人可以帮忙吗?
  • 现在代码中缺少很多部分。你能提供一个minimal, complete verifiable example吗?不要只是从您现有的应用程序中复制,而是使用最少的代码从头开始重现问题。
  • @FrankvanPuffelen 我尝试添加尽可能多的代码(希望阅读起来不会太无聊),添加了 cmets 并解释了错误发生的位置。你能看一下吗?

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


【解决方案1】:

在绑定ViewHolder 时,不要考虑position 是最终的,而是创建对密钥的最终引用:

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

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多