英文:
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.
This is the code that I am using..
Database references
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) {
}
});
}
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("questions posts")
.child(postid)
.child("group")
.child(groupname)
.child("requests")
.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("questions posts").child(postid).child("group").child(groupname).child("requests").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("questions posts").child(postid).child("group").child(groupname).child("requests")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论