2

I am having trouble iterating through a dataSnapshot. When I debug the code, I can see that the dataSnapshot has a size of 2 and it contains the correct key / value pairs. But it seems as if it doesn't see the key / value pairs as children, I guess.

My Database

//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot) {
    spacecrafts.clear();
    //for (DataSnapshot ds : dataSnapshot.getChildren())
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        Spacecraft spacecraft = new Spacecraft();
        try {
            spacecraft.setLevel(Integer.parseInt(ds.getKey()));
        } catch (NumberFormatException ex) {
            System.out.println("Could not parse " + ex);
        }

        long v = (long) ds.getValue();
        spacecraft.setNumCorrect(v);

        //Spacecraft spacecraft=ds.getValue(Spacecraft.class);
        spacecrafts.add(spacecraft);
    }

    Collections.sort(spacecrafts, new Comparator<Spacecraft>() {
        @Override
        public int compare(Spacecraft spacecraft, Spacecraft t1) {
            return Integer.valueOf(spacecraft.getLevel()).compareTo(t1.getLevel()); // To compare integer values
        }
    });
}

//RETRIEVE
public ArrayList<Spacecraft> retrieve() {
    String myUserId = acct.getId();

    DatabaseReference LevelsRef = db.child("levels/uid/").child(myUserId);
    Query queryRef = LevelsRef;

    queryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            //fetchData(dataSnapshot);
            fetchData(dataSnapshot);
            adapter.notifyDataSetChanged();
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return spacecrafts;
}

So when the code gets to the for (DataSnapshot ds : dataSnapshot.getChildren()), it just goes right to the sort code and the spacecrafts arraylist is empty. So dataSnapshot.getChildren is empty.

I tried removing the for loop and just processing the first "Record" in the dataSnapshot and it processes fine. Does anyone have any idea why this is happening and what I can do about it?

Edit: The solution

public ArrayList<Spacecraft> retrieve() {
    String myUserId = acct.getId();

    //DatabaseReference LevelsRef = db.child("levels/uid/" + myUserId + "/");
    DatabaseReference LevelsRef = db.child("levels/uid/");
    Query queryRef = LevelsRef.orderByChild(myUserId);

    queryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            //fetchData(dataSnapshot);
            fetchData(dataSnapshot);
            adapter.notifyDataSetChanged();
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return spacecrafts;
}
David.Warwick
  • 620
  • 1
  • 9
  • 28
  • 1
    Recheck your code, you missed a minor part. You skipped a node hence it is empty. – user7568042 Feb 20 '17 at 04:28
  • What node did I miss? I am new to json, so did I structure the query incorrectly? – David.Warwick Feb 20 '17 at 04:32
  • 1
    yeah query is incorrect, you're pointing at the key you want but not taking any values from that key. – user7568042 Feb 20 '17 at 04:39
  • What I want to get is, for a given user, the Key represents the game Level number. The Value represents the number of points for that level. So I need to put the Level number and the points into a listview. How do I take values from that key? I am not sure what I should change. Thanks for your patience. – David.Warwick Feb 20 '17 at 04:45
  • The `return spacecrafts` statement at the end of `retrieve()` will not return valid results. The listener callbacks fire asynchronously and will not yet have executed when `spacecrafts` is returned. Further explanation at this answer: http://stackoverflow.com/a/41409942/4815718 – Bob Snyder Feb 20 '17 at 05:25
  • @user7568042, I figured it out. See my original post for the solution. Thank you. – David.Warwick Feb 20 '17 at 05:30
  • @qbix. My problem has been resolved and I am getting valid results. See my edited post. I will take your advice though and read through that other post. Maybe there will be a post when I have more data that my results will become invalid. – David.Warwick Feb 20 '17 at 05:36

0 Answers0