如何通过引用它的子节点从firebase中删除节点?

时间:2018-02-21 07:32:18

标签: java android firebase firebase-realtime-database

"Subscription" : {
    "1519197182611" : {
      "address" : "Mumbai",
      "dateTime" : "Feb 21, 2018 12:42:57 PM",
      "name" : "ABC",
      "phone" : "1264897809",
      "uIdpId" : "123456"
    },
    "1519197186551" : {
      "address" : "Mumbai",
      "dateTime" : "Feb 21, 2018 12:42:57 PM",
      "name" : "DCF",
      "phone" : "1264897809",
      "uIdpId" : "7897"
    },
    "1519197360198" : {
      "address" : "Mumbai",
      "dateTime" : "Feb 21, 2018 12:45:54 PM",
      "name" : "XYZ",
      "phone" : "1264897809",
      "uIdpId" : "45656"
    }
  }

我想删除名称为ABC的节点。 那我该怎么办呢?我被困在这里。

1 个答案:

答案 0 :(得分:2)

试试这个:

DatabaseReference data = FirebaseDatabase.getInstance().getReference().child("Subscription");
data.orderByChild("name").equalTo(ABC).addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      for(DataSnapshot data: dataSnapshot.getChildren()){
            data.getRef().removeValue();

                  }

            }

        @Override
       public void onCancelled(DatabaseError databaseError) {

               }
          });

快照位于子Subscription,然后您使用orderByChild("name").equalTo(valuehere),这是名称应该等于ABC的条件。

然后使用for循环在随机pushids中迭代,getRef()将为您提供此源位置的引用,removeValue()将删除该节点。