使用OnClick在recyclerview中删除项目。 Firebase安卓

时间:2016-12-03 03:05:26

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

我在firebase中检索数据,如果用户点击了recyclerview中的项目,但是当我在recyclerview中找到2个或3个项目时,我想删除它,我得到像这样的堆栈跟踪。有时我点击它的项目是错误的。如何解决?

 E/AndroidRuntime: FATAL EXCEPTION: main 
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.firebase.ui.database.FirebaseArray.getItem(FirebaseArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getRef(FirebaseRecyclerAdapter.java:150)
at com.myapp.maecea.ipon4.Expenses$3$1$2.onClick(Expenses.java:274)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

adapter = new FirebaseRecyclerAdapter < ExpenseData, myViewHolder > (
     ExpenseData.class, R.layout.listitem, myViewHolder.class, mRef.orderByChild("date").equalTo(date)) {

     @Override
     protected void populateViewHolder(final myViewHolder viewHolder, ExpenseData model, final int position) {
         viewHolder.DateText.setText(model.getDate());
         viewHolder.ExpenseTitleText.setText(model.getExpensetitle());
         viewHolder.AmountText.setText(String.valueOf(String.format("%.2f", model.getAmount())));

         viewHolder.mView.setOnClickListener(new View.OnClickListener() {@
             Override
             public void onClick(final View v) {
                 AlertDialog.Builder builder = new AlertDialog.Builder(Expenses.this);
                 builder.setMessage("Gusto mo ba talaga burahin ito, katulad ng pagkalimot niya sayo?").setCancelable(false)
                     .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             int selectedItems = position;
                             for (int i = selectedItems; i >= 1; i++) {
                                 adapter.getRef(i).removeValue();
                                 adapter.notifyItemRemoved(i);
                             }
                         }
                     })
                     .setNegativeButton("No", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             dialog.cancel();
                         }
                     });
                 AlertDialog dialog = builder.create();
                 dialog.setTitle("Expenses");
                 dialog.show();
             }
         });
     }
 };    

 recyclerView.setAdapter(adapter);

2 个答案:

答案 0 :(得分:0)

IndexOutOfBoundsException 因为您增加了索引值。在for循环中将i++更改为i--。此代码将所选项目中的元素移除到selectedItems&gt; = 1。

for (int i = selectedItems; i >= 1; i--) {
    adapter.getRef(i).removeValue();
    adapter.notifyItemRemoved(i);
}

如果您只想删除一个项目(循环视图上的点击项目)意味着删除for循环简单使用

adapter.getRef(i).removeValue();
adapter.notifyItemRemoved(i);

答案 1 :(得分:0)

考虑使用viewHolder.getAdapterPosition()代替position