java.lang.ArrayIndexOutOfBoundsException:length = 10;从OnChildRemoved Firebase数据库获取index = -1

时间:2019-03-06 06:22:47

标签: java android firebase-realtime-database android-recyclerview recycler-adapter

我正试图从我的recyclerview中删除数据,并让其在移动ui中也进行更新。这是一个 android 应用。我可以删除数据,但是我总是必须离开该页面,然后在进行更改时返回。我尝试对 onChildRemoved 方法进行一些修改,但在进行此更改后开始出现崩溃。我该怎么办?下面是我的代码。

MainActivity.java

DatabaseReference databaseReference;
List<DataSnapshot> listData;
RecyclerView recyclerView;
MyCart.MyAdapter adapter;


recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);

listData = new ArrayList<>();
adapter = new MyCart.MyAdapter(listData);
adapter.setHasStableIds(true);

GetDataFirebase();

private void GetDataFirebase() {
    databaseReference = FirebaseDatabase.getInstance().getReference()
            .child("Customers")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("My Cart");
    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            Items students = dataSnapshot.getValue(Items.class);

            listData.add(dataSnapshot);

            recyclerView.setAdapter(adapter);

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            int index = listData.indexOf(dataSnapshot);
            listData.remove(index);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

适配器类

public class MyAdapter extends RecyclerView.Adapter<MyCart.MyAdapter.ViewHolder> {

    List<DataSnapshot> list;

    public MyAdapter(List<DataSnapshot> List) {
        this.list = List;
    }

    @Override
    public void onBindViewHolder(MyCart.MyAdapter.ViewHolder holder, int position) {
        final DataSnapshot studentSnapshot = list.get(position);

        final Items students = studentSnapshot.getValue(Items.class);

        final String list_user_id = studentSnapshot.getKey();

        holder.item_name.setText(students.getItem_name());
        holder.item_price.setText(students.getItem_price());

        Picasso.with(holder.item_image.getContext()).load(students.getItem_image()).placeholder(R.drawable.no_image_two).into(holder.item_image);

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseReference prod_ref = FirebaseDatabase.getInstance().getReference().child("Customers").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("My Cart").child(list_user_id);
                prod_ref.removeValue();
                adapter.notifyDataSetChanged();
                Toast.makeText(MyCart.this, "Item removed from cart", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @NonNull
    @Override
    public MyCart.MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_item_layout, parent, false);

        return new MyCart.MyAdapter.ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView item_name, item_price;
        ImageView item_image;
        ImageView delete;

        public ViewHolder(View itemView) {
            super(itemView);

            item_name = itemView.findViewById(R.id.item_name);
            item_price = itemView.findViewById(R.id.item_price);

            item_image = itemView.findViewById(R.id.item_image);

            delete = itemView.findViewById(R.id.delete_item);

        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}

2 个答案:

答案 0 :(得分:0)

当列表中不存在dataSnapshot时,它将返回-1。列表的索引从0开始。因此,这是您遇到的错误。您需要在此处添加防护检查,例如:

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            int index = listData.indexOf(dataSnapshot);
            if(index > 0) {
               listData.remove(index);
            }
            adapter.notifyDataSetChanged();
        }

答案 1 :(得分:0)

holder.delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DatabaseReference prod_ref = FirebaseDatabase.getInstance().getReference().child("Customers").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("My Cart").child(list_user_id);
            prod_ref.removeValue();

            //add/modify these two lines here 
            list.remove(position);
            notifyDataSetChanged();


        }
    });

还有另一件事需要做 //将此敬酒移动到onChildRemoved方法,并从那里清除其他代码

Toast.makeText(MyCart.this, "Item removed from cart", Toast.LENGTH_SHORT).show();

喜欢

@Override
    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
        //remove these line these will raise the exception and Toast here 
        int index = listData.indexOf(dataSnapshot);
        listData.remove(index);
        adapter.notifyDataSetChanged();
    }