I am using the Firebas Realtime Database for Android and I would like to read data from my database from the node ("Table") "Ratings". Alls entries in that node are from the type Item_FirebaseDB_Rating
and all of them have a variable called orderID
. Now I would like to query the Firebase Database once to check, whether there is an entry in it with a specific orderID
. This should just be a single check, whenever I call the method checkIfItemAlreadyRated_FirebaseDB
and it should not continuously check for updates (as many other Firebase Database queries do).
For that I use the following method:
public static boolean checkIfItemAlreadyRated_FirebaseDB (int orderID) {
boolean result = false;
// Not the whole name of the database is given in the URL for privacy reasons.
DatabaseReference rootRef = FirebaseDatabase.getInstance("https://...firebasedatabase.app").getReference();
ValueEventListener ratingListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Item_FirebaseDB_Rating rating = dataSnapshot.getValue(Item_FirebaseDB_Rating.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.e("LogTag", "loadPost:onCancelled", databaseError.toException());
}
};
Query query = FirebaseDatabase.getInstance("https://...firebasedatabase.app").getReference("Ratings")
.orderByChild("orderID").equalTo(orderID);
query.addListenerForSingleValueEvent(ratingListener);
if(query != null) {
result = true;
}
return result;
}
I have 2 question on that:
- When I call this method, I get an error that I don't understand " com.google.firebase.database.DatabaseException: Class com.example.td.bapp.Item_FirebaseDB_Rating does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped."
- I define a query to get the
Item_FirebaseDB_Rating rating
from the database. Now my question is, how can I get this object out of the Listener and use it in the other part of the method? So how can I get the results of the query? There should be something likequery.getResults()
or similar. But I could not find it.