我理解错误是如何导致的,因为在随机删除行后数组索引似乎存在问题,所以如何解决它。任何帮助将不胜感激。
这是我的代码
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<String> reminderMessage;
private ArrayList<String> receiverName;
public DataAdapter(ArrayList<String> reminderMessage,ArrayList<String> receiverName) {
this.reminderMessage = reminderMessage;
this.receiverName = receiverName;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final DataAdapter.ViewHolder viewHolder, final int i) {
viewHolder.reminderText.setText(reminderMessage.get(i));
viewHolder.receiverName.setText(receiverName.get(i));
viewHolder.button_reject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rejectItem(viewHolder.getAdapterPosition());
}
});
}
@Override
public int getItemCount() {
return reminderMessage.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView reminderText;
private TextView receiverName;
Button button_reject;
public ViewHolder(View view) {
super(view);
reminderText = (TextView)view.findViewById(R.id.remindertext);
receiverName = (TextView)view.findViewById(R.id.receiverName);
button_reject=(Button)view.findViewById(R.id.button_reject);
}
}
private void rejectItem(int position) {
DatabaseReference mdatabase = FirebaseDatabase.getInstance().getReference();
try {
mdatabase.child("reminders").child(messageKeys.get(position)).child("status").setValue("reject");
Log.d("in the loop","writing");
} catch (Exception e) {
e.printStackTrace();
}
reminderMessage.remove(position);
receiverName.remove(position);
messageKeys.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, reminderMessage.size());
}
}
这是错误
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.import2, PID: 6891
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.admin.import2.DataAdapter.onBindViewHolder(DataAdapter.java:47)
at com.example.admin.import2.DataAdapter.onBindViewHolder(DataAdapter.java:27)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2969)
at android.view.View.measure(View.java:19734)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19734)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19734)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.example.admin.import2-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.admin.import2-2/lib/x86, /system/lib, /vendor/lib]]
Application terminated.
答案 0 :(得分:0)
为什么不直接使用这样的某个对象列表呢?
class Data{
String name, message;
}
加号
public DataAdapter(List<Data> data) {
this.data = data;
}
通过使用两个列表,您只使用一个大小,同时删除和获取两个列表,这只会在以后导致错误
除此之外,问题中没有定义messageKeys
,所以请调试一下
如果您想使用Firebase,请给FirebaseUI一个机会