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

huangapple go评论84阅读模式
英文:

How to delete a node from the firebase real-time database after creating another node with same IDs

问题

我正在尝试在点击警报对话框的接受选项时删除一个节点请求,同时也在成功创建节点联系时删除它。然而,我得到的结果是节点联系如预期创建,但节点请求却没有被删除。

以下是我使用的代码:
数据库引用

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和FirebaseRecyclerAdapter的设置:

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

FirebaseRecyclerAdapter<Users, RequestsViewHolder> adapter = new FirebaseRecyclerAdapter<Users, RequestsViewHolder>(options) {
    // ...
};

在adapter的onBindViewHolder方法中,处理数据和点击事件:

@Override
protected void onBindViewHolder(@NonNull final RequestsViewHolder holder, int position, @NonNull Users model) {
    // ...

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // ...

            builder.show();
        }
    });
}

在点击对话框中的“接受”选项时执行的操作:

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, "参与者已添加", 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, "参与者已删除", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
    });
}

希望这有助于解决你的问题。如果有任何其他帮助需要,请随时提问。

英文:

I am trying to delete a node requests when the accept option of the alert dialog is clicked,and also when the node contacts created successfully. What I am getting however is that the node contact is being created as expected, but the node requests is not.
如何在创建了具有相同ID的另一个节点后,从Firebase实时数据库中删除节点。

This is the code that I am using..
Database references

joinRequestsRef =  FirebaseDatabase.getInstance().getReference().child(&quot;questions posts&quot;).child(postid).child(&quot;group&quot;).child(groupname).child(&quot;requests&quot;).child(currentUserID);
usersRef = FirebaseDatabase.getInstance().getReference(&quot;users&quot;);
contactsRef = FirebaseDatabase.getInstance().getReference().child(&quot;questions posts&quot;).child(postid).child(&quot;group&quot;).child(groupname).child(&quot;contacts&quot;)/*.child(currentUserID)*/;
FirebaseRecyclerOptions&lt;Users&gt; options = new FirebaseRecyclerOptions.Builder&lt;Users&gt;()
.setQuery(joinRequestsRef, Users.class)
.build();
FirebaseRecyclerAdapter&lt;Users, RequestsViewHolder&gt; adapter = new FirebaseRecyclerAdapter&lt;Users, RequestsViewHolder&gt;(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(&quot;request_type&quot;).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(&quot;received&quot;)) {
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(&quot;name&quot;).getValue()).toString();
String requestProfileImage = Objects.requireNonNull(dataSnapshot.child(&quot;profilepictureurl&quot;).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[]{&quot;accept&quot;, &quot;cancel&quot;};
AlertDialog.Builder builder = new AlertDialog.Builder(JoinRequestsActivity.this);
builder.setTitle(&quot;Join Request&quot;);
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(&quot;contact&quot;).setValue(&quot;saved&quot;).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()){
contactsRef.child(list_user_id).child(currentUserID).child(&quot;contact&quot;).setValue(&quot;saved&quot;).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()){
joinRequestsRef.child(currentUserID).child(list_user_id).removeValue().addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()){
joinRequestsRef.child(list_user_id).child(currentUserID).removeValue().addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()){
Toast.makeText(JoinRequestsActivity.this, &quot;Participator added&quot;, Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}
});
}
}
});
}
if (which == 1){
joinRequestsRef.child(currentUserID).child(list_user_id).removeValue().addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()){
joinRequestsRef.child(list_user_id).child(currentUserID).removeValue().addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
if (task.isSuccessful()){
Toast.makeText(JoinRequestsActivity.this, &quot;Participator Deleted&quot;, Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}
});
builder.show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}

Any help will be appreciated

答案1

得分: 2

在插入具有相同根节点的值之后,将此代码编写如下:

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

deleteRef.removeValue();
英文:

Write this code after you insert the value with the same root node

deleteRef = FirebaseDatabase.getInstance()
.getReference()
.child(&quot;questions posts&quot;)
.child(postid)
.child(&quot;group&quot;)
.child(groupname)
.child(&quot;requests&quot;)
.child(USER_ID)
deleteRef.removeValue();

答案2

得分: 1

你拥有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");
英文:

You are having the joinRequestRef till the currentUserID

joinRequestsRef =  FirebaseDatabase.getInstance().getReference().child(&quot;questions posts&quot;).child(postid).child(&quot;group&quot;).child(groupname).child(&quot;requests&quot;).child(currentUserID);

And in the code, you are again accessing the currentUserID:

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

Also, inside currentUserID you will not get list_user_id child. So this will not work.

joinRequestsRef.child(list_user_id).child(currentUserID)

Try till requests

joinRequestsRef =  FirebaseDatabase.getInstance().getReference().child(&quot;questions posts&quot;).child(postid).child(&quot;group&quot;).child(groupname).child(&quot;requests&quot;)

huangapple
  • 本文由 发表于 2020年8月21日 17:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63520083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定