0

So I have a game lobby where when a user creates a game, their username and how much in game currency they are betting on the game appears on the screen for all players to see in a RecyclerView.

So in this case, a user named 'Brent_MB' created a game

enter image description here

But the issue is the player put a wager of 2.00 on the game and it won't appear. (2.50 is the default number that appears)

This is what my database structure looks like

enter image description here

So, since this database is so dynamic, it literally changes each time someone wants to play a game and it gets deleted when the game starts, how can I go about assigning the wager value to the slot that currently says '2.50'

This is what I have tried to do:

I have created a callback for the wager variable like this,

public void getWager(final WagerCallback wagerCallback) {
    FCGames.child("What do I put here to check each node every time a node is added")
            .child("wager").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                wagerr = (long) dataSnapshot.getValue();
                String wagerD = Double.toString(wagerr);
                wage = wagerD.endsWith("0");

                wagerCallback.onCallback(wagerD);
        }

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

I also have a Callback interface with this but it doesn't seem relevant.

and then I call this method in my RecyclerView class like this:

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    String userName = mData.get(position);
    holder.userName.setText(userName);
    holder.bind(mData.get(position), position, listener);

        fcl.getWager(new FlipCoinLobby.WagerCallback() {
            @Override
            public void onCallback(String wager) {
                holder.wager.setText(wager);
            }
        });

}

As you can see, in this line I am stuck on what to put as the first child so that it can check each child when it is added and get that wager value. \

FCGames.child("What do I put here to check each node every time a node is added").child("wager")

What should I put there so that it can get the wager value from each node and appropriately assign it to the text view area?

Each initial node in the FCGames database is stored in an ArrayList called openGames. I can show how nodes are added to that list if necessary

wages.add(wagerD);

for(int i=0; i <= openGames.size(); i++) {
   wagerCallback.onCallback(wages.get(i));
}

1 Answer 1

1

Since you don't know the key of the child node you're trying to load, you have two options:

  1. Load all child nodes of FCGames.
  2. Load the first/last child node of FCGames.

I'll show the second option, as the first is easy to derive from that:

FCGames.orderByKey()
       .limitToFirst(1)
       .addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
            wagerr = snapshot.child("wager").getValue(Long.class);
            String wagerD = Double.toString(wagerr);
            wage = wagerD.endsWith("0");

            wagerCallback.onCallback(wagerD);
        }
    }

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

The biggest differences are:

  • I use a query to get the first key only.
  • I then need to use a loop in onDataChange, since queries can have multiple results (event though this one won't).
7
  • Hi Frank, thanks for the detailed response! My questions are: are the first and second options you offered synonymous in what they end up doing? I implemented the second option, like you showed, and if there are 2 games created then they both display the wager of the first game. Is that was the .limitToFirst(1) is doing?
    – brent
    Nov 6, 2018 at 1:12
  • The onDataChange on the code I shared will only be called with a single child node. If you want it to be called for each child node, remove the .orderByKey() .limitToFirst(1) from my code (which brings you to the first solution). I highly recommend spending some time reading about Firebase queries btw, as what I'm doing here are fairly basic examples of that. Nov 6, 2018 at 1:17
  • Thank you Frank. I know, I have a lot to learn and generally I prematurely ask questions (before doing enough research), I am working on that and appreciate your patience and assistance. Every problem you, and other users, help me solve is another thing I learn about various things.
    – brent
    Nov 6, 2018 at 1:20
  • You're welcome Brent. The best you can do to help us help you is isolating your problem in a so-called MCVE. To learn what that is, and how it's helpful for both you and us, see how to create a minimal, complete, verifiable example. Nov 6, 2018 at 1:31
  • I am having an issue with this still, and I am not sure why. The for loop always sets the wager value to the last node in my database. So if i have a user with the name 'zach' create a game, and a user named 'brent' create a game, the wager's both reflect the wager of 'zach' because he is the last node. I would understand why this was happening if I had .limitToLast() in place but I do not.
    – brent
    Nov 6, 2018 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.