Reputation: 85
I have a firebase database that is loaded via Async, and I can verify that all data exists in the correct nodes, but for some reason I can't figure out how to call for the data stored in the particular node.
I've tried .addChildEventListener and .addValueEventListener, but nothing seems to allow for my global object to be loaded with the details at the particular node.
Here's a quick snapshot of json looks like for each node.
{
"movies" : {
"297802" : {
"movieBackdrop" : "/5A2bMlLfJrAfX9bqAibOL2gCruF.jpg",
"movieFavorite" : 0,
"movieID" : 297802,
"movieLanguage" : "en",
"movieOverview" : "Once home to the most advanced civilization on Earth, the city of Atlantis is now an underwater kingdom ruled by the power-hungry King Orm. With a vast army at his disposal, Orm plans to conquer the remaining oceanic people -- and then the surface world. Standing in his way is Aquaman, Orm's half-human, half-Atlantean brother and true heir to the throne. With help from royal counselor Vulko, Aquaman must retrieve the legendary Trident of Atlan and embrace his destiny as protector of the deep.",
"moviePopularity" : 116,
"moviePosterPath" : "/5Kg76ldv7VxeX9YlcQXiowHgdX6.jpg",
"movieReleaseDate" : "2018-12-07",
"movieTitle" : "Aquaman",
"movieVoteAverage" : 6.8,
"movieVoteCount" : 3668
},
"299536" : {
"movieBackdrop" : "/bOGkgRGdhrBYJSLpXaxhXVstddV.jpg",
"movieFavorite" : 0,
"movieID" : 299536,
"movieLanguage" : "en",
"movieOverview" : "As the Avengers and their allies have continued to protect the world from threats too large for any one hero to handle, a new danger has emerged from the cosmic shadows: Thanos. A despot of intergalactic infamy, his goal is to collect all six Infinity Stones, artifacts of unimaginable power, and use them to inflict his twisted will on all of reality. Everything the Avengers have fought for has led up to this moment - the fate of Earth and existence itself has never been more uncertain.",
"moviePopularity" : 109,
"moviePosterPath" : "/7WsyChQLEftFiDOVTGkv3hFpyyt.jpg",
"movieReleaseDate" : "2018-04-25",
"movieTitle" : "Avengers: Infinity War",
"movieVoteAverage" : 8.3,
"movieVoteCount" : 11448
},
I've created my Firebase Database with the movieID being the key value, rather than the generated values, since my ids will always be unique.
Now when I try to get the snapshot stored as my object via the below method, I keep getting a null error pointing to my object currentMovie when I try to extrapolate some of the objects data to update the UI. I know that when I am checking for .equalTo(String.valueOf(movieID) - it is passing a value that correlates to a valid value that should match up correctly, but I keep getting the null error.
mFirebaseDatabase = FirebaseDatabase.getInstance();
mMovieDatabaseReference = mFirebaseDatabase.getReference().child("movies");
Query movieQuery = mMovieDatabaseReference.orderByChild("movieID").equalTo(String.valueOf(movieID));
movieQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
currentMovie = dataSnapshot.getValue(Movie.class);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Any thoughts or ideas on why I'm having so much difficulty here?
Upvotes: 3
Views: 435
Reputation: 598837
You're comparing strings and integers.
In your JSON it shows that movieID
is a numeric value:
"movieID" : 297802
In the code you do:
mMovieDatabaseReference.orderByChild("movieID").equalTo(String.valueOf(movieID))
So that means you're comparing integers and strings, which always returns false. The solution is to compare numbers with numbers:
mMovieDatabaseReference.orderByChild("movieID").equalTo(movieID)
Upvotes: 2