0

I'm trying to get the count of "msanID" but it always returns the number as '0'. I'm using a separate class (ManageNodeName) to record the data and retrieving them back at list view. I suspect the error is in the method of getting the String MSAN_NAMESP. Any lead may help. Thanks!

My Firebase Parent "MSAN MTCE"

List View

databaseNodes = FirebaseDatabase.getInstance().getReference("MSAN List");

@Override
protected void onStart() {
    super.onStart();


    databaseNodes.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            manageNodeList.clear();

            for(DataSnapshot nodesnapshot: dataSnapshot.getChildren()){
                progressDialog.dismiss();

                ManageNodeName manageNodeName = nodesnapshot.getValue(ManageNodeName.class);
                manageNodeList.add(manageNodeName);

                MSAN_NAMESP = manageNodeName.getNodeID();
            }
            ManageNodeList adapter = new ManageNodeList(ManageSelectMSAN.this, manageNodeList);
            manageListViewMSANs.setAdapter(adapter);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });


    query = FirebaseDatabase.getInstance().getReference("MSAN MTCE")
            .orderByChild("msanID")
            .equalTo(MSAN_NAMESP);


    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            int size = (int) dataSnapshot.getChildrenCount();

            ManageNodeName.setCount(size);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Komee
  • 13
  • 2
  • Your code accesses `getReference("MSAN List")`, while the JSON has `MSAN MTCE`. – Frank van Puffelen May 20 '20 at 14:41
  • @FrankvanPuffelen Yes. I'm trying to get the name from "MSAN List" parent and query the other parent "MSAN MTCE" for it's count. – Komee May 20 '20 at 17:02
  • When you run this code in the debugger, which line isn't doing what you'd expect it to do? – Frank van Puffelen May 20 '20 at 17:21
  • @FrankvanPuffelen, I'm trying to figure out sir. It's hard cause there are no errors. May be my whole method is wrong. Could you understand my intention? I'm trying to get the name from one value event listener and use it to query another. Can you suggest me a method for that? – Komee May 20 '20 at 17:41

1 Answers1

0

Data is loaded from Firebase (and most cloud APIs) asynchronously. While the data is being loaded, your main code continues to run, so that the user can continue to use the app. Then when the data is available, your onDataChange is called with that data.

This means that all code that needs the data, needs to either be inside onDataChange or be called from there.

So:

databaseNodes.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        manageNodeList.clear();

        for(DataSnapshot nodesnapshot: dataSnapshot.getChildren()){
            progressDialog.dismiss();

            ManageNodeName manageNodeName = nodesnapshot.getValue(ManageNodeName.class);
            manageNodeList.add(manageNodeName);

            MSAN_NAMESP = manageNodeName.getNodeID();

            query = FirebaseDatabase.getInstance().getReference("MSAN MTCE")
                    .orderByChild("msanID")
                    .equalTo(MSAN_NAMESP);

            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    int size = (int) dataSnapshot.getChildrenCount();

                    ManageNodeName.setCount(size);
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    throw databaseError.toException(); // never ignore errors
                }
            });

        }
        ManageNodeList adapter = new ManageNodeList(ManageSelectMSAN.this, manageNodeList);
        manageListViewMSANs.setAdapter(adapter);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // never ignore errors
    }
});

Also see: getContactsFromFirebase() method return an empty list for a longer example, and alternative approaches.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks for the lead! I have already tried this but it returns me the count of the last "MSAN_NAMESP" of list. May be how I set the value to ManageNodeName.class is wrong? I'm trying ManageNodeName.setCount(size); – Komee May 20 '20 at 18:47
  • I have figured out an alternative method of querying the count at my listview activity instead of here. It works fine. Thanks again! – Komee May 22 '20 at 16:40