dylan
dylan

Reputation: 153

Android Firebase db datasnapshot pulling information from wrong db directory

In my Android app I am using Google Firebase to store information in the database.

I need to iterate through the information, and below for-loop is being used

public void showDataLobReq(DataSnapshot dataSnapshot){

    for(DataSnapshot ds : dataSnapshot.child("Lobby_Requests").getChildren()){
        System.out.println("asdfasdfasdfasdfasdf"+ds.getValue());
        game = ds.child(userID).child("game").getValue(String.class);
        console = ds.child(userID).child("console").getValue(String.class);
        mic = ds.child(userID).child("mic").getValue(String.class);
        players = ds.child(userID).child("players").getValue(String.class);
    }


}

You may notice that I put '.child("Lobby_Requests")' after dataSnapshot. this is because the dataSnapshot takes a snapshot of the whole database, so I must go into the subdirectory "Lobby_Requests" because that is where the information I need to iterate through is.

Putting this '.child()' in is being problematic.

I print to the console what the dataSnapshot contains in the first line of the for loop and with .child("Lobby_Requests") it is pulling information from the directory "Lobbies" in the actual database, which is a completely different directory.

Yet, when I remove the '.child()' completely it gives me a view of the whole database like it should. Why is it doing this?

Code for listener:

nRef = mFirebaseDatabase.getReference();
    nRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                showDataLobReq(dataSnapshot);
            } else {

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here is JSON:

{


"Games" : {
    "Forza 6" : {
      "Consoles" : {
        "PC" : true,
        "Xbox 1" : true,
        "Xbox 360" : true
      },
      "FilePathName" : "forza6",
      "Genres" : {
        "Racing" : true
      },
      "Live Lobbies" : 0,
      "Name" : "Forza 6"
    },
    "Minecraft" : {
      "Consoles" : {
        "PC" : true,
        "Xbox 1" : true,
        "Xbox 360" : true
      },
      "FilePathName" : "minecraft",
      "Genres" : {
        "Adventure" : true,
        "Creation" : true,
        "Open World" : true
      },
      "Live Lobbies" : 0,
      "Name" : "Minecraft"
    }
  },
  "Lobbies" : {
    "Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
      "Messages" : {
        "-Kq6-1HsMvElEXZZyCIk" : {
          "messageText" : "hey",
          "messageTime" : 1501208519771,
          "messageUser" : ""
        }
      },
      "console" : "Origin",
      "game" : "Minecraft",
      "leader" : "Cd6lVd2XMUYoLH6b0xoHsrfXMud2",
      "mic" : "Mic",
      "note" : "2345",
      "players" : "4"
    },
    "KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
      "console" : "Steam",
      "game" : "Forza 6",
      "mic" : "No Mic",
      "note" : "Hey Join Here!",
      "players" : "2"
    },
    "hpWkq0D8clPReUetOq9Xtmc4V582" : {
      "Messages" : {
        "-Kq5a0kX305lFCRTSM_G" : {
          "messageText" : "hello",
          "messageTime" : 1501201701014,
          "messageUser" : ""
        },
        "-Kq5asufOWQwtmyNJrQ7" : {
          "messageText" : "hey",
          "messageTime" : 1501201926941,
          "messageUser" : ""
        }
      },
      "console" : "Xbox One",
      "game" : "Minecraft",
      "leader" : "hpWkq0D8clPReUetOq9Xtmc4V582",
      "mic" : "Mic",
      "note" : "kjhg",
      "players" : "4"
    }
  },
  "Lobby_Requests" : {
    "Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
      "Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
        "console" : "Xbox One",
        "game" : "Forza 6",
        "mic" : "Mic",
        "players" : "5"
      }
    },
    "KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
      "KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
        "console" : "Steam",
        "game" : "Forza 6",
        "mic" : "No Mic",
        "players" : "2"
      }
    },
    "hpWkq0D8clPReUetOq9Xtmc4V582" : {
      "hpWkq0D8clPReUetOq9Xtmc4V582" : {
        "console" : "Xbox One",
        "game" : "Minecraft",
        "mic" : "Mic",
        "players" : "4"
      },
      "players" : "4"
    }
  },
  "users" : {
    "8cHrNCybwjO3PIUKxyOLiAqxJBv1" : {
      "gamertag" : "thedylan",
      "uname" : "thedood"
    },
    "Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
      "gamertag" : "dmdylan",
      "uname" : "ninja goat"
    },
    "KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
      "gamertag" : "skaner",
      "uname" : "asdf"
    },
    "YvYEIiCBUSYKTviVyWpLHdyDIFw1" : {
      "gamertag" : "joejoe",
      "uname" : "Jifflingly"
    },
    "ZmX9yIZ6MNguQa1S3MaYNcxfK2b2" : {
      "gamertag" : "dmkaner",
      "uname" : "dmkaner"
    },
    "hpWkq0D8clPReUetOq9Xtmc4V582" : {
      "gamertag" : "dmkaner",
      "uname" : "dmkaner"
    },
    "t21ncnuRmeV4F7RknETBisMrxS42" : {
      "gamertag" : "asdf",
      "uname" : "asdf"
    }
  }
}

Upvotes: 0

Views: 136

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

The problem in your code is that you are pushing your data twice and there is no need for this.

  "Lobby_Requests" : {
      "Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
          "Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : { //This is wrong

If you'll change the way in which you add data to the Firebase database by pushing that data only once, your code will work for fine. Your database should look like this:

"Lobby_Requests" : {
  "Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
    "console" : "Xbox One",
    "game" : "Forza 6",
    "mic" : "Mic",
    "players" : "5"
  },

As you probably see, there is only one pushed key.

Upvotes: 1

Related Questions