Updating a List Size for all users

brent

New Member
#1
I am trying to make it so that when a user creates an open game, in my Firebase Database the name of the game is the size of the list. So the very first game created the name of it will = 0, and if another user creates a game then the game will be labeled 1 and so on.

I have it set up right now so that the games are labeled the size of the game list, but the list isn't really updating. The games keep getting called '0' because it thinks the list is empty even though I have visual confirmation in the app that there are items being added to the list.

So my question is: How can I make it so the list continuously updates each time a game is added, and how can I make it so that it updates for all users and not just the user who created the game?

This is what I have setup right now. Here are the variables I am using for the list and the integer getting the list size
Mã:
ArrayList<String> openGames = new ArrayList<>();
int gameSlot = openGames.size();
Here is what I use to name the game when it is created.
Mã:
gameMaker = new GameMaker(hp.uid, userName, wagerD, gameSlot);

FirebaseDatabase.getInstance().getReference("FCGames").child(Integer.toString(gameSlot))
        .setValue(gameMaker).addOnCompleteListener...
And this is what I have to add the game to the list.
Mã:
cgRef.child(Integer.toString(gameSlot)).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            openGames.add(userName);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });
So again my question is how can I make this list update correctly and how can I make it update for all users on the app?
Edit
Here is what I did with my onChangeData
Mã:
    cgRef.child(Integer.toString(gameSlot)).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            wager = (String) dataSnapshot.child("wager").getValue();
            gameSlot = openGames.size();
            adapter.notifyDataSetChanged();
        }
and now the openGames.add is in my createGameLobby method.
Mã:
        FirebaseDatabase.getInstance().getReference("FCGames").child(Integer.toString(gameSlot))
            .setValue(gameMaker).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    openGames.add(userName);
                Toast.makeText(FlipCoinLobby.this, "Game creation successful.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(FlipCoinLobby.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });
^^ that is just the important snippit from the method. And then I have an onClickListener that creates that calls that method when a button is pressed
 

Admin

Administrator
Thành viên BQT
#2
In below code segment, you did that openGames.add(username) in onDataChange listener. I think it is an incorrect use of this ondatachange function
Mã:
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    openGames.add(userName);
    adapter.notifyDataSetChanged();
}
please use this to get data from db and update your data. Then push your data to db. You didn't use snapshot value also. You can get most recently updated data from dataSnapshot . Use it to update user data then push it to db
 
Top