如何在创建另一个具有相同ID的节点后从firebase实时数据库中删除节点

vhmi4jdf  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(414)

我正在尝试在单击警报对话框的“接受”选项时删除节点请求,以及在成功创建节点联系人时删除节点请求。然而,我得到的是,节点联系是按预期创建的,但节点请求不是。

这是我正在使用的代码。。数据库引用

joinRequestsRef =  FirebaseDatabase.getInstance().getReference().child("questions posts").child(postid).child("group").child(groupname).child("requests").child(currentUserID);
    usersRef = FirebaseDatabase.getInstance().getReference("users");
    contactsRef = FirebaseDatabase.getInstance().getReference().child("questions posts").child(postid).child("group").child(groupname).child("contacts")/*.child(currentUserID)*/;

FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
            .setQuery(joinRequestsRef, Users.class)
            .build();

    FirebaseRecyclerAdapter<Users, RequestsViewHolder> adapter = new FirebaseRecyclerAdapter<Users, RequestsViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final RequestsViewHolder holder, int position, @NonNull Users model) {

            holder.itemView.findViewById(R.id.user_requests_accept_button).setVisibility(View.VISIBLE);
            holder.itemView.findViewById(R.id.user_requests_reject_button).setVisibility(View.VISIBLE);

            final String list_user_id = getRef(position).getKey();
            DatabaseReference getTypeRef = getRef(position).child("request_type").getRef();

            getTypeRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                      String type = Objects.requireNonNull(dataSnapshot.getValue()).toString();
                      if (type.equals("received")) {
                          assert list_user_id != null;
                          usersRef.child(list_user_id).addValueEventListener(new ValueEventListener() {
                              @Override
                              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                 if (dataSnapshot.exists()) {
                                     String requestFullName = Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString();
                                     String requestProfileImage = Objects.requireNonNull(dataSnapshot.child("profilepictureurl").getValue()).toString();

                                     holder.fnTextView.setText(requestFullName);
                                     Glide.with(getApplication()).load(requestProfileImage).into(holder.imageView);

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

                                         AlertDialog.Builder builder = new AlertDialog.Builder(JoinRequestsActivity.this);
                                         builder.setTitle("Join Request");

                                         builder.setItems(options, new DialogInterface.OnClickListener() {
                                             @Override
                                             public void onClick(DialogInterface dialog, int which) {
                                                 if (which == 0){
                                                     contactsRef.child(currentUserID).child(list_user_id).child("contact").setValue("saved").addOnCompleteListener(new OnCompleteListener<Void>() {
                                                         @Override
                                                         public void onComplete(@NonNull Task<Void> task) {
                                                             if (task.isSuccessful()){
                                                                 contactsRef.child(list_user_id).child(currentUserID).child("contact").setValue("saved").addOnCompleteListener(new OnCompleteListener<Void>() {
                                                                     @Override
                                                                     public void onComplete(@NonNull Task<Void> task) {
                                                                         if (task.isSuccessful()){
                                                                             joinRequestsRef.child(currentUserID).child(list_user_id).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                                                                 @Override
                                                                                 public void onComplete(@NonNull Task<Void> task) {
                                                                                     if (task.isSuccessful()){
                                                                                         joinRequestsRef.child(list_user_id).child(currentUserID).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                                                                             @Override
                                                                                             public void onComplete(@NonNull Task<Void> task) {
                                                                                                 if (task.isSuccessful()){
                                                                                                     Toast.makeText(JoinRequestsActivity.this, "Participator added", Toast.LENGTH_SHORT).show();
                                                                                                 }
                                                                                             }
                                                                                         });
                                                                                     }
                                                                                 }
                                                                             });
                                                                         }
                                                                     }
                                                                 });
                                                             }
                                                         }
                                                     });
                                                 }
                                                 if (which == 1){
                                                     joinRequestsRef.child(currentUserID).child(list_user_id).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                                         @Override
                                                         public void onComplete(@NonNull Task<Void> task) {
                                                             if (task.isSuccessful()){
                                                                 joinRequestsRef.child(list_user_id).child(currentUserID).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                                                     @Override
                                                                     public void onComplete(@NonNull Task<Void> task) {
                                                                         if (task.isSuccessful()){
                                                                             Toast.makeText(JoinRequestsActivity.this, "Participator Deleted", Toast.LENGTH_SHORT).show();
                                                                         }
                                                                     }
                                                                 });
                                                             }
                                                         }
                                                     });
                                                 }
                                             }
                                         });

                                         builder.show();
                                     }
                                 });
                              }

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

                              }
                          });
                      }

                }

                }

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

                }
            });
        }

任何帮助都将不胜感激

xam8gpfp

xam8gpfp1#

你现在有 joinRequestRef 直到 currentUserID ```
joinRequestsRef = FirebaseDatabase.getInstance().getReference().child("questions posts").child(postid).child("group").child(groupname).child("requests").child(currentUserID);

在代码中,您再次访问 `currentUserID` :

joinRequestsRef.child(currentUserID).child(list_user_id).removeValue()

还有,里面 `currentUserID` 你不会得到 `list_user_id` 孩子。所以这行不通。

joinRequestsRef.child(list_user_id).child(currentUserID)

试到 `requests` ```
joinRequestsRef =  FirebaseDatabase.getInstance().getReference().child("questions posts").child(postid).child("group").child(groupname).child("requests")
kkbh8khc

kkbh8khc2#

在插入具有相同根节点的值后编写此代码

deleteRef = FirebaseDatabase.getInstance()
                            .getReference()
                            .child("questions posts")
                            .child(postid)
                            .child("group")
                            .child(groupname)
                            .child("requests")
                            .child(USER_ID)

deleteRef.removeValue();

相关问题