gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:57 26 4
gpt4 key购买 nike

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

我的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)

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

最佳答案

与其在绑定(bind) 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);
}
});
}

关于android - 删除数据时 FirebaseRecyclerAdapter 不更新项目索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com