第一次或第二次单击onviewholder项时,我的应用程序不会崩溃,但是第三次​​之后,我出现此错误

时间:2019-03-03 14:41:42

标签: android firebase firebase-realtime-database firebaseui

这是一个电子商务应用程序,在我的一项活动中,我可以跟踪我的订单,当我单击收到的订单时,在查看器中显示它的弹出对话框,询问我是否发货,如果需要,它将从firebase中删除订单和产品列表...目前,它正在从firebase中删除订单列表,当我添加时,它也会删除购物车列表。在运行我的应用程序后,当我单击第一个订单并将其标记为出厂时,其移除订单和购物清单很好,但是在第二个项目中,如果我单击以将其标记为出厂,则我的应用程序崩溃。

我在这里添加了调试代码

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.commerce.daily.dailycommerce, PID: 5132
                  java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
                      at java.util.ArrayList.get(ArrayList.java:437)
                      at com.firebase.ui.common.BaseObservableSnapshotArray.getSnapshot(BaseObservableSnapshotArray.java:70)
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.getRef(FirebaseRecyclerAdapter.java:112)
                      at com.commerce.daily.dailycommerce.AdminNewOrdersActivity$1$2$1.onClick(AdminNewOrdersActivity.java:104)
                      at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1067)
                      at android.widget.AdapterView.performItemClick(AdapterView.java:318)
                      at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
                      at android.widget.AbsListView$PerformClick.run(AbsListView.java:3134)
                      at android.widget.AbsListView$3.run(AbsListView.java:4049)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我的活动在下面

private RecyclerView ordersList;
    private DatabaseReference ordersRef;
    private DatabaseReference cartRef;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_new_orders);

        ordersRef = FirebaseDatabase.getInstance().getReference().child("Orders");
        cartRef = FirebaseDatabase.getInstance().getReference().child("Cart List").child("Admin View");

        ordersList = findViewById(R.id.orders_list);
        ordersList.setLayoutManager(new LinearLayoutManager(this));

        populateFeed(); // for start main process in place of on start method to avoid app crash

    }


    private void populateFeed()
    {
        FirebaseRecyclerOptions<Products> options1 =
                new FirebaseRecyclerOptions.Builder<Products>()
                        .setQuery(cartRef, Products.class)
                        .build();


        FirebaseRecyclerOptions<AdminOrders> options =
                new FirebaseRecyclerOptions.Builder<AdminOrders>()
                        .setQuery(ordersRef, AdminOrders.class)
                        .build();
        FirebaseRecyclerAdapter<AdminOrders, AdminOrdersViewHolder> adapter =
                new FirebaseRecyclerAdapter<AdminOrders, AdminOrdersViewHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull AdminOrdersViewHolder holder, final int position, @NonNull final AdminOrders model)
                    {
                        holder.userName.setText("Name:" + model.getName());
                        holder.userPhoneNumber.setText("Phone:" + model.getPhone());
                        holder.userTotalPrice.setText("Total Amount = $" + model.getTotalAmount());
                        holder.userDateTime.setText("Order at:" + model.getDate() + "," + model.getTime());
                        holder.userShippingAddress.setText("Shipping Address:" + model.getAddress() + " ," + model.getCity());
                        holder.paymentStatus.setText("Payment Status:" + model.getPaymentState() );
                        holder.paymentID.setText("Payment ID :" + model.getPaymentID());
                        holder.ShowOrdersBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v)
                            {
                                String uID = getRef(position).getKey();



                                Intent intent = new Intent(AdminNewOrdersActivity.this, AdminUserProductsActivity.class);
                                intent.putExtra("uid", uID);
                                startActivity(intent);
                            }
                        });

                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v)
                            {
                                CharSequence options[] = new CharSequence[]
                                        {
                                                "Yes",
                                                "No"
                                        };

                                AlertDialog.Builder builder = new AlertDialog.Builder(AdminNewOrdersActivity.this);
                                builder.setTitle("Have you shipped this order products ?");
                                builder.setItems(options, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        if(which==0)
                                        {
                                            String uID = getRef(position).getKey();

                                            RemoveOrder(uID);




                                        }
                                        else
                                        {
                                            finish();
                                        }
                                    }
                                });

                                builder.show();
                            }
                        });

                    }

                    @NonNull
                    @Override
                    public AdminOrdersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orders_layout, parent, false);
                        return new AdminOrdersViewHolder(view);
                    }
                };

        ordersList.setAdapter(adapter);
        adapter.startListening();
    }




    public static class AdminOrdersViewHolder extends RecyclerView.ViewHolder
    {
        public TextView userName, userPhoneNumber, userTotalPrice, userDateTime, userShippingAddress, paymentStatus, paymentID;
        public Button ShowOrdersBtn;

        public AdminOrdersViewHolder(View itemView)
        {
            super(itemView);
            paymentStatus = itemView.findViewById(R.id.payment_status);
            paymentID = itemView.findViewById(R.id.payment_id);
            userName = itemView.findViewById(R.id.order_user_name);
            userPhoneNumber = itemView.findViewById(R.id.order_phone_number);
            userTotalPrice = itemView.findViewById(R.id.order_total_price);
            userDateTime = itemView.findViewById(R.id.order_date_time);
            userShippingAddress = itemView.findViewById(R.id.order_address_city);
            ShowOrdersBtn = itemView.findViewById(R.id.show_all_products_btn);
        }
    }

    private void RemoveOrder(final String uID)
    {

        ordersRef.child(uID).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task)
            {
                if(task.isSuccessful())
                {
                    RemoveCartItem(uID);
                }

            }
        });

    }
    private void RemoveCartItem(String uID)
    {
        cartRef.child(uID).removeValue();
    }

}

0 个答案:

没有答案