永远不会调用OnDataChange FIrebase数据库

时间:2017-02-22 13:16:21

标签: android firebase firebase-realtime-database

我正在使用firebase数据库,需要在数据库中创建唯一的子节点才能启动一对一聊天。我打电话给FireBaseDatabase.updateChildren(RoomName)为2位用户设置单人间。

我在调用FireBaseDatabase.addListenerForSingleValueEvent(new ValueEventListener()

时遇到问题

并且在此监听器下,即使在public void onCancelled(DatabaseError databaseError)

上也没有任何方法调用

这是我的代码

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Snackbar.make(view, "Hello I am SnackBar", Snackbar.LENGTH_LONG).setAction("Action", null).show();
        Log.d(TAG, "User Session Value " + userSession);
        userSession = new UserSession(getActivity(), "Pref_FileName");
        Log.d(TAG, "User Session Value After Init " + userSession);
        final Map<String, Object> RoomName = new HashMap<>();
        RoomTitle = userSession.getPhoneNO() + "-" + UserData.getPhone();
        otherUserRoom = UserData.getPhone() + "-" + userSession.getPhoneNO();
        Log.d(TAG, "Room Title Should Be " + RoomTitle);


        DatabaseReference FireBaseDatabase =  FirebaseDatabase.getInstance().getReference();



        Log.d(TAG, "Chat room name from Database " + db.toString());  //GETTING THIS VALUE IN LOG 

        FireBaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.hasChild(RoomTitle)) {
                    intent2.putExtra("Get_Other",RoomTitle);
                    Log.d(TAG,"Room Exists No Need To Create It "+RoomTitle); // THIS LOG NEVER GENERATE
                    RoomNameFinal=RoomTitle;

                }
                else if (dataSnapshot.hasChild(otherUserRoom)){
                    intent2.putExtra("Get_Other",otherUserRoom);
                    Log.d(TAG,"Room Exists No Need To Create It "+otherUserRoom);// THIS LOG NEVER GENERATE
                    RoomNameFinal=otherUserRoom;


                }

                else {
                    Log.d(TAG,"None Of the above Room exist so Create one with RoomTitle "+RoomName);// THIS LOG NEVER GENERATE
                    intent2.putExtra("Get_Other",RoomTitle);
                    RoomName.put(RoomTitle, "");
                    FireBaseDatabase.updateChildren(RoomName);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG,"On Cancelled Calling "+databaseError.getDetails());  // THIS LOG NEVER GENERATE
                Log.d(TAG,"On Cancelled Calling "+databaseError.getMessage());  // THIS LOG NEVER GENERATE
                Log.d(TAG,"On Cancelled Calling "+databaseError.toString());    // THIS LOG NEVER GENERATE

            }


        });



        intent2.putExtra("Get_Phone", UserData.getPhone());
        intent2.putExtra("Get_Image", UserData.getImage());
        intent2.putExtra("Get_Name", UserData.getName());
        Log.d(TAG,"Room Name Final "+RoomNameFinal);
        startActivity(intent2);

    }
});

编辑:我在Firebase数据库中添加子项的位置 enter image description here

1 个答案:

答案 0 :(得分:0)

您只是忘记在函数getReference()中指明数据库引用的名称。

例如,如果要获取消息对象,则需要将DatabaseReference设置为

DatabaseReference myRef = database.getReference("message");

我的数据库的网址如下:

https://myproject.firebaseio.com/message

此网址显示在Firebase信息中心的RealTimeDatabase标签的顶部。

希望得到这个帮助。