当没有数据时,不会调用Firebase onChildAdded

时间:2016-11-16 11:36:41

标签: android firebase firebase-realtime-database

我想检查里面是否有孩子。当我有数据时,如果没有被调用的孩子,它会进入OnChildAdded。我想知道是否有任何数据?我该如何检查?

ref.orderByKey().startAt(todayDate).endAt(tomorrowDate).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            System.out.println("does the child exist? " + dataSnapshot.child("matches").exists());
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                AvailableRidesFirebaseObject rides = postSnapshot.getValue(AvailableRidesFirebaseObject.class);

                AvailableRidesFirebaseObject availableRidesFirebaseObject = new AvailableRidesFirebaseObject();
                availableRidesFirebaseObject.setAdded_on(rides.getAdded_on());
                availableRidesFirebaseObject.setCode(rides.getCode());
                availableRidesFirebaseObject.setTime(rides.getTime());
                availableRidesFirebaseObject.setDate_of_ride(rides.getDate_of_ride());
                availableRidesFirebaseObject.setDay(rides.getDay());
                availableRidesFirebaseObject.setDropoff_location(rides.getDropoff_location());
                availableRidesFirebaseObject.setDropoff_coordinate(rides.getDropoff_coordinate());
                availableRidesFirebaseObject.setFullname(rides.getFullname());
                availableRidesFirebaseObject.setOffered_by(rides.getOffered_by());
                availableRidesFirebaseObject.setPickup_location(rides.getPickup_location());
                availableRidesFirebaseObject.setPickup_coordinate(rides.getPickup_coordinate());
                availableRidesFirebaseObject.setSeats(rides.getSeats());
                availableRidesFirebaseObject.setShare(rides.getShare());
                availableRidesFirebaseObject.setVechile_type(rides.getVechile_type());
                availableRidesList.add(availableRidesFirebaseObject);


                filteredList.add(rides);
                Logger.e("list" ,filteredList.toString());
            }

            try{
                adapter = new RecyclerViewAdapter(getActivity(), availableRidesList);
                availableRideListView.setHasFixedSize(true);
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
                availableRideListView.setLayoutManager(mLayoutManager);
                availableRideListView.setAdapter(adapter);
                loading.setVisibility(View.GONE);

            }catch(NullPointerException ne){
                ne.printStackTrace();
                /*if(availableRidesList.size()==0){
                    loading.setVisibility(View.GONE);
                }*/
            }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }




    });

1 个答案:

答案 0 :(得分:0)

顾名思义onChildAdded()只有在将一个子项添加到集合中时才会被调用。如果没有孩子,则不会被召唤。

要检测没有子节点的情况,可以附加值事件侦听器。

Query query = ref.orderByKey()
                 .startAt(todayDate)
                 .endAt(tomorrowDate)
                 .limitToFirst(1);
query.addValueEventListener(new ValueEventListener() {
  public void onDataChange(DataSnapshot snapshot) {
    if (snapshot.getChildrenCount() == 0) {
      System.out.println("No children");
    };
  }
  public void onCancelled(DatabaseError error) {
    throw error.toException();
  }
});