Arn*_*hra 3 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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这一行得到以下IndexOutOfBoundsException:
String key= getRef(position).getKey();
Run Code Online (Sandbox Code Playgroud)
如果我在列表中有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)
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
而不是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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
552 次 |
| 最近记录: |