This is how my Firebase db looks like.
subscriber
u36eD7PsOaf6uo0CGuGPBjC3Y223
children: false
empty: false
id:"u36eD7PsOaf6uo0CGuGPBjC3Y223"
name: "JACKSON"
smoker:false
I want to find the record which matches id = u36eD7PsOaf6uo0CGuGPBjC3Y223.
Below is my code and its not able to retrieve the record
Query recentPostsQuery = mRef.orderByChild("id").equalTo("u36eD7PsOaf6uo0CGuGPBjC3Y223");
recentPostsQuery.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e("Count ", "" + dataSnapshot.getChildrenCount());
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
subscriber = postSnapshot.getValue(Subscriber.class);
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Log.e count statement is returning 1 record which is correct, but looks like my query is wrong, that is why I am unable to fetch record.
Your query must return String value not a object of Subscriber, Complete object of Subscriber exsist at node - u36eD7PsOaf6uo0CGuGPBjC3Y223
// With return String value which is u36eD7PsOaf6uo0CGuGPBjC3Y223
mRef.orderByChild("id").equalTo("u36eD7PsOaf6uo0CGuGPBjC3Y223");
Related
Dear Friends,
I am accessing data from a Firebase database, however I am unable to get a list of my data.
I am getting the following exception:
E/UncaughtException: com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.sg.rapid.Models.AlaramData`
here is my code:
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
AlaramData usersList = dataSnapshot.getValue(AlaramData.class);
String name = usersList.getStatus();
childList.add(usersList);
// here you can access to name property like university.name
}
Log.d("", "Value is: " + childList);
//Create a List of Section DataModel implements Section
sections.add(new SectionHeader(childList, "2018", 1));
adapterRecycler.notifyDataSetChanged();
}
Thanks in advance.
Reading the error message, we can understand that the method is returning an ArrayList of your elements, then I noticed you used the wrong variable there.
It should be using postSnapshot instead of dataSnapshot. Try this:
AlaramData usersList = postSnapshot.getValue(AlaramData.class);
I have solved the issue by changing the json structure in firebase database.
the code as follows:
// Read from the database
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
Log.d("", "onChildChanged:" + dataSnapshot.getKey());
sections.clear();
childList.clear();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
AlaramData alaramData = postSnapshot.getValue(AlaramData.class);
int x = 0;
// here you can access to name property like university.name
childList.add(alaramData);
}
Log.d("", "Value is: " + childList);
//Create a List of Section DataModel implements Section
sections.add(new SectionHeader(childList, "2018", 1));
adapterRecycler.notifyDataChanged(sections);
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("", "Failed to read value.", error.toException());
}
});
i am trying to get the key that i used to push information to my table. Is there any other way to JUST get the key itself.
i am trying to get the red marked key
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
if(snapshot.exists()){
Toast.makeText(getActivity(), dataSnapshot.getValue().toString() , Toast.LENGTH_LONG).show();// try to get ID
}
}}
this is what i keep on getting
is there any other possible way on how i could get the red mark, directly?
DatabaseReference bdRef = FirebaseDatabase.getInstance().getReference()
.child("Lobby").orderBy("lobbyname").equalTo("admin lobby");
bdRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
Toast.makeText(getActivity(), child.getKey().toString() , Toast.LENGTH_LONG).show();// try to get ID
}
}
I have a database which holds timestamped entries for lifestyle quizzes that a user takes. The schema is as follows where the children of the timestamp are key value pairs corresponding to the users inputs.
-EvcKZHBZ4CVo9yAdlP7ldadCZS03
-2018-03-19 12:19:49
- age: "20"
- exercise_total: "0"
...
-2018-03-18 12:32:44
- age: "20"
- exercise_total: "15"
...
I have an object made called heartScore with member variables corresponding to all of the children of the timestamp. How do I return the data under the first date entry into a object of type heartScore if I don't know the exact date that the quiz was taken?
This should do the trick:
databaseReference.child("userId").limitToFirst(1)
In practice:
FirebaseReference ref = FirebaseDatabase.getInstance().getReference()
ref.child(userId).limitToFirst(1).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
// There will only be one child
HeartScore score = snapshot.getValue(HeartScore.class);
// Return the HeartScore object in a callback or whatever else you want
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I'm trying to get the names of the sections in my Firebase database and add it to a arraylist, but it only returns null.
This is my code:
mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/");
ValueEventListener sectionListner = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("sections", "onDataChange");
if (dataSnapshot.exists()) {
Log.d("sections", "snapshot exists" );
for (DataSnapshot sectionSnapshot : dataSnapshot.getChildren()) {
if (sectionSnapshot != null) {
Section section = sectionSnapshot.getValue(Section.class);
Log.d("sections", "Section created: " + section.getName());
} else
Log.d("sections", "sections null");
}
}
}
#Override
public void onCancelled(DatabaseError error) {
Log.w("failedSnap", "Failed to read value.", error.toException());
}
};
mSectionReference.addValueEventListener(sectionListner);
And this is the result of the logcat:
onDataChange
snapshot exists
Section created: null
Section created: null
To solve this, please change this line of code:
mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/");
with
mSectionReference = database.getReference().child("apartments").child("B3").child("sections");
The slashes are not needed when you pass the child name as an argument.
SOLUTION:
I found that even though that there is a element in the path, it doesn't mean that there is a "object" there.
I implemented a new hashmap that saves the name of the section as a key and value, making the structure look like this:
I am trying to query a child node value but I don't know parent key, I want to query for Firebase Adapter. I want to access userid node under area but I don't know the Id of parent i.e -KqPJMsjSb5CbPcq4nXv
Here is the snapshot of Record:
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userIdRef = rootRef.child("areas").child(userId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String areaId = ds.child("areaId").getValue(String.class);
Boolean booked = ds.child("booked").getValue(Boolean.class);
Integer bookingHour = ds.child("bookingHour").getValue(Integer.class);
//and so on
Log.d("TAG", areaId + " / " + booked + " / " + bookingHour);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userIdRef.addListenerForSingleValueEvent(eventListener);
In which userId is the id of the user that said that is not missing.
When you execute a query against the Firebase Database, there will potentially be multiple results.
So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.