3
我有一個項目列表,每個項目都有一個名稱和一個刪除按鈕。當刪除數據時,FirebaseRecyclerAdapter不更新項目索引
我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方法,這需要通過&設置上刪除的ImageView的clickListener的FirebaseRecyclerAdapter檢索的對象。
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
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,2刪除後)我應該着手解決這個問題嗎?
你不應該叫'notifyItemRemoved()'或適配器上的相似。只需從數據庫中刪除項目,剩下的就會自動發生。 –
最初我已經刪除它們,但搜索所以我發現它們應該被添加。無論如何,我的錯誤仍然存在,即使我刪除它們。 –
@FrankvanPuffelen你或你的團隊中的某人可以幫忙嗎? –