更新firebase中的子值

时间:2017-04-30 12:49:12

标签: android firebase firebase-realtime-database

我的Firebase结构:

 "Requests" : {
"-KiyT2fiL8dE3jh7ccWT" : {
  "4pGOtqaTskarMnvz058UBpYbW1y2" : {
    "address" : "Street 12",
    "phone" : "05****699",
    "status" : "status",
    "subject" : "subjectText",
    "teacherName" : "nameText",
    "time_date" : "30 Apr 2017 1:46:43 p.m.",
    "uid_user" : "4pGOtqaTskarMnvz058UBpYbW1y2"
  }

我正在尝试更新子" status"的值,如何转到"status"中的rootRef? 这是我的代码

Acceptable.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Unacceptable.setEnabled(false);
                    Toast.makeText(context, " this Request has been Acceptable " , Toast.LENGTH_LONG).show();
                    rootRef = FirebaseDatabase.getInstance().getReference("Requests");

                     rootRef.child("Requests").child("uid").child("uid_user").orderByChild("status").addValueEventListener(new ValueEventListener() {
                         @Override
                         public void onDataChange(DataSnapshot dataSnapshot) {

                             final String  status = dataSnapshot.getValue(String.class);
                             System.out.println(status);
                             rootRef.child("status").setValue("Your Request has been Acceptable");


                         }

                         @Override
                         public void onCancelled(DatabaseError databaseError) {

                         }
                     });
                    Toast.makeText(context, " this Request has been Acceptable " , Toast.LENGTH_LONG).show();
                    Acceptable.setEnabled(false);


                                      }
            });

2 个答案:

答案 0 :(得分:1)

为什么每个用户都有推送ID?这使得有必要进行查询,我怀疑它是否需要。一个更简单的解决方案是将用户存储在他们的UID下:

Users
  4pG0...y2
    address: "..."
    phone: "..."
    status: "status"

使用此结构,您可以使用以下命令修改用户的状态:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

rootRef.child("Requests").child(uid).child("status").setValue("new status");

答案 1 :(得分:0)

不完全确定db的结构如何,但这可能会有所帮助

public void onClick(View v) {
                    Unacceptable.setEnabled(false);
                    Toast.makeText(context, " this Request has been Acceptable " , Toast.LENGTH_LONG).show();
                    FirebaseDatabase.getInstance().getReference("Requests").child("status").setValue("Your Request has been Acceptable");

                    Toast.makeText(context, " this Request has been Acceptable " , Toast.LENGTH_LONG).show();
                    Acceptable.setEnabled(false);

}