Firebase ValueListener告诉我节点不存在

时间:2017-02-05 15:13:27

标签: android firebase firebase-realtime-database

我很抱歉,如果我错过了一些东西,但过去几个小时我一直在这里。我的Firebase数据库中明显存在一个节点,但附加到它的监听器拒绝这么认为。

JSON树

"liveSessions" : {
"-KaOX9bL9dbNOY0iSHP7" : {
  "latitude" : 24.8763847,
  "location" : {
    ".priority" : "tkrtw2s56n",
    "g" : "tkrtw2s56n",
    "l" : [ 24.8763847, 67.0771205 ]
  },
  "longitude" : 67.0771205,
  "participantLimit" : 20,
  "sessionCreatedBy" : "test notifications",
  "sessionCreatedByKey" : "MeIPeBAvKOWwDc2BFz97XbWmgzJ2",
  "sessionDuration" : "7:20:00",
  "sessionImgUri" : url,
  "sessionSportType" : "Throwball",
  "sportIconIdName" : "throwball",
  "timeStampCreated" : -1484339604568,
  "usersRequesting" : {
    "moO4LEDl0MVQufW6UFBRkywsYyE3" : "moO4LEDl0MVQufW6UFBRkywsYyE3"
  }
}
}

Java代码

public class ManageLiveSessionFragment extends Fragment {

String sessionKey;

ArrayList<String> requestedUserList;
ArrayList<String> joinedUserList;

RecyclerView requestedUsersRecycler;

DatabaseReference sessionRef;
private SessionRequestedUsersAdapter sessionRequestedUsersAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_manage_live_session, container, false);

    Bundle bundle = getArguments();
    sessionKey = bundle.getString("SESSION_KEY");
    Log.d("sessionKey", sessionKey);

    sessionRef = FirebaseDatabase.getInstance().getReference();

    requestedUserList = new ArrayList<>();
    joinedUserList = new ArrayList<>();

    requestedUsersRecycler = (RecyclerView) rootView.findViewById(R.id.manageSession_requesting);
    requestedUsersRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

    sessionRequestedUsersAdapter = new SessionRequestedUsersAdapter(requestedUserList, getContext(), sessionKey);
    requestedUsersRecycler.setAdapter(sessionRequestedUsersAdapter);

    sessionRef.child("liveSessions").child(sessionKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (!dataSnapshot.exists()){
                Log.d("Does it exist?", "nope");
            }
            for (DataSnapshot user : dataSnapshot.child("usersRequesting").getChildren()){
                requestedUserList.add(user.getKey());
                Log.d("Added", "One user added");
                sessionRequestedUsersAdapter.notifyDataSetChanged();
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return rootView;
}

调试代码只是表明节点(它的快照)不存在。

1 个答案:

答案 0 :(得分:0)

要访问usersRequesting节点,您应该使用以下内容:

sessionRef = FirebaseDatabase.getInstance().getReference()
.child("liveSessions")
.child("usersRequesting")
.addValueEventListener( ... );

给我们一个sessionKey的例子

出现了错误。 在node .child(sessionKey),JSON Tree的其余部分将是:

"-KaOX9bL9dbNOY0iSHP7" : {
  "latitude" : 24.8763847,
  "location" : {
    ".priority" : "tkrtw2s56n",
    "g" : "tkrtw2s56n",
    "l" : [ 24.8763847, 67.0771205 ]
  }
显然,

没有usersRequesting节点

在帖子开头尝试我的例子