从RecyclerAdapter上的firebase中删除数据

时间:2018-04-30 03:17:37

标签: android firebase android-recyclerview removechild

我在RecyclerViewAdapter上删除了一个带有PopupMenu的列表项,但是当该项被删除时,它会保留在Firebase上。

  

这是在RecyclerAdapter上的onBindViewHolder上

holder.opcionesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //creating a popup menu
            PopupMenu popup = new PopupMenu(context, holder.opcionesButton);
            //inflating menu from xml resource
            popup.inflate(R.menu.menu_edit);
            //adding click listener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.edit_item:
                            //handle menu1 click
                            break;
                        case R.id.delete_item:

                            tareasList.remove(position);
                            notifyItemRemoved(position);
                            Toast.makeText(context, "Tareas eliminada!", Toast.LENGTH_SHORT).show();
                            break;

                    }
                    return false;
                }
            });
            //displaying the popup
            popup.show();
        }
    });
  

这是在MainFragment

  final Query tareasQuery = mTareasDatabase.child(userId);
        tareasQuery.addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Tareas tareas = new Tareas();
                tareas.setFecha(dataSnapshot.getValue(Tareas.class).getFecha());
                tareas.setDescripcion(dataSnapshot.getValue(Tareas.class).getDescripcion());
                tareas.setLista(dataSnapshot.getValue(Tareas.class).getLista());
                tareas.setPrioridad(dataSnapshot.getValue(Tareas.class).getPrioridad());
                mTareasList.add(tareas);
                mTareasAdapter.notifyDataSetChanged();

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                String tareasId = dataSnapshot.getValue(Tareas.class).getTareasId();
                mTareasDatabase.child(userId).child(tareasId).removeValue();
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                String tareasId = dataSnapshot.getValue(Tareas.class).getTareasId();
                mTareasDatabase.child(userId).child(tareasId).removeValue();

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

一切正常,但不是从firebase中删除它

解决方案

同时从firebase和回收者视图中删除该项目

case R.id.delete_item:
                            FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
                            String tareasId = tareas.getTareasId();
                            final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                            String userId = user.getUid();
                            DatabaseReference databaseReference = firebaseDatabase.getReference("Tareas")
                                    .child(userId).child(tareasId);
                            databaseReference.removeValue();

                            tareasList.remove(position);
                            notifyItemRemoved(position);
                            Toast.makeText(context, "Tareas eliminada!", Toast.LENGTH_SHORT).show();
                            break;

2 个答案:

答案 0 :(得分:0)

好的,我解决了。我只需要在删除项目上添加所有数据库引用,同时将它从回收站视图和数据库中删除

 case R.id.delete_item:
                            FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
                            String tareasId = tareas.getTareasId();
                            final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                            String userId = user.getUid();
                            DatabaseReference databaseReference = firebaseDatabase.getReference("Tareas")
                                    .child(userId).child(tareasId);
                            databaseReference.removeValue();

                            tareasList.remove(position);
                            notifyItemRemoved(position);
                            Toast.makeText(context, "Tareas eliminada!", Toast.LENGTH_SHORT).show();
                            break;

答案 1 :(得分:0)

这是因为您只从本地列表中删除。您还必须从firebase中删除该值。删除使用代码

firebaseRef.removeValue();

在你的firebase中,你必须显示你要删除的值。