我正在尝试在单击警报对话框的“接受”选项时删除节点请求,以及在成功创建节点联系人时删除节点请求。然而,我得到的是,节点联系是按预期创建的,但节点请求不是。
这是我正在使用的代码。。数据库引用
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) {
}
});
}
任何帮助都将不胜感激
2条答案
按热度按时间xam8gpfp1#
你现在有
joinRequestRef
直到currentUserID
```joinRequestsRef = FirebaseDatabase.getInstance().getReference().child("questions posts").child(postid).child("group").child(groupname).child("requests").child(currentUserID);
joinRequestsRef.child(currentUserID).child(list_user_id).removeValue()
joinRequestsRef.child(list_user_id).child(currentUserID)
kkbh8khc2#
在插入具有相同根节点的值后编写此代码