I want to get the value of isSeen node in Android Studio. The M6F2mJjQ8uGPXAynZzV is an id of the message and change message to message. And I want to get this value in an Adapter class.
So you want to get a boolean, convert to string and compare it to Another string:
If that what you want then this is the answer for you:
//this is the string that you want to compare against
String yourString = "true";
//this is your reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Chats");
//make a listener
ValueEventListener listener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//loop through the random key IDs
for(DataSnapshot ds : dataSnapshot.getChildren()){
//get the boolean "isSeen"
boolean isSeen = ds.child("isSeen").getValue(Boolean.class);
//convert boolean to string
String isSeenString = String.valueOf(isSeen);
//compare isSeenString with your String
if(isSeenString.equals(yourString)){
//they are equal
}else{
//they are not equal
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
//add the listener
ref.addValueEventListener(listener);
If you want to get the isSeen value then try the following:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Chats");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
Boolean isSeen = data.child("isSeen").getValue(Boolean.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
Iterate inside Chats node and then you can retrieve isSeen.
Related
I have a database in fire-base. In that I have a child node named "item1", under item1 i have two values
name
prize
I want to retrieve the name of item1 and I want to put it in a string called "foodname". How can I do that?
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("menu");
Here I tried but did not find solution
String foodname; //this string should get the value as "diary milk"
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("menu").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//This will loop through all items. Add variables to arrays or lists as required
for (DatasnapShot snap : dataSnapshot.getChildren())
{
foodname = dataSnapshot.child("name").getValue().toString();
String prize = dataSnapshot.child("prize").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You can fetch each child individually like such. Or you can make use of a Model or a Hashmap to fetch all of the data and then fetch the data you would like based on the Key
Using below code you can get food name
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("menu");
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String foodName = snapshot.child("name").getValue().toString();
String foodPrice = snapshot.child("prize").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Retrieving any data from Firebase requires a correct database reference and eventListeners that can work for what you want.
To know more about eventListeners visit this link.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("menu").child("item1");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// this code will get only details from item1
foodname = dataSnapshot.child("name").getValue(String.class);
String price = dataSnapshot.child("prize").getValue(Integer.class);
//if you want details from multiple items, you have to loop over the child nodes like this
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
foodname = snapshot.child("name").getValue().toString();
String price = snapshot.child("prize").getValue().toString();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I am developing an android application and now I am stuck in retrieving data from particular nodes like I want to retrieve only one value from each nodes. The database structure shows below.
How can I retrieve the second unique id that created by Firebase database?
First create a POJO class to get the values you want, it should be writen the same way as you have them in Firebase.
public class AppointmentsPojo {
private String appointmentStuts;
public AppointmentsPojo(){
}
public String getAppointmentStuts() {
return appointmentStuts;
}
public void setAppointmentStuts(String appointmentStuts) {
this.appointmentStuts = appointmentStuts;
}
}
Then just loop inside Appointments to get each appointmentStuts
mDatabase.child("Appointments").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Looping inside Appointments to get each appointmentsStuts
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
AppointmentsPojo ap = snapshot.getValue(AppointmentsPojo.class);
//Getting each appointmentStuts
String appointmentStuts = ap.getAppointmentStuts();
//To get each father of those appointments
String key = snapshot.getKey();
Log.e("Data: " , "" + appointmentStuts );
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
Where mDatabase is
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
To get the value of appointmentStuts only from the first object, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Appointments").child(PostKey).limitToFirst(1);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String appointmentStuts = ds.child("appointmentStuts").getValue(String.class);
Log.d(TAG, appointmentStuts);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
query.addListenerForSingleValueEvent(valueEventListener);
Data stored in firebase:
29-06-2018(date)
-AAAA
-25142(jobno)
-Park station(address)
-BMW(model)
-BBBB
-85142(jobno)
-Base station(address)
-Ford(model)
Here I want all the children under -BBBB. Don't want to loop through AAAA. How to get directly the child of BBBB. I'm having data (date, BBBB). Just want to get jobno, address, model of BBBB. Please suggest me a solution.
My code is here
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference pwd = database.child("29-06-2018").child("BBBB");
pwd.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String a = ds.child("jobno").getValue(String.class);
String b = ds.child("address").getValue(String.class);
String c = ds.child("model").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError error) {
}
});
You're listening to a single child /29-06-2018/BBBB. By looping over dataSnapshot.getChildren() you're looping over each property, and then try to reach a child property for each. That won't work, so you should get rid of the loop in onDataChange:
pwd.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot ds) {
String a = ds.child("jobno").getValue(String.class);
String b = ds.child("address").getValue(String.class);
String c = ds.child("model").getValue(String.class);
}
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference pwd = database.child("29-06-2018").child("BBBB");
pwd.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String a = dataSnapshot.child("jobno").getValue(String.class);
String b = dataSnapshot.child("address").getValue(String.class);
String c = dataSnapshot.child("model").getValue(String.class);
}
#Override
public void onCancelled(DatabaseError error) {
}
});
I have a list of objects on my Firebase database, and the key for each object is a timestamp. I can get the last one using limitToLast(1), and the data snapshot I get is:
{ key = time_stamps, value = {1510768441759={id=f454445c6e880df31e, coordinates={latitude=-4.1461734, longitude=20.5992437}}} }
Where 1510768441759 is the timestamp.
How do I read the coordinates? I've tried to convert the value to a POJO, but I cannot, because the timestamp is unknown to me, and in a POJO it would be the name of a property…
Thanks.
If you do it without a parent POJO but only with coordinates pojo you can just do the following by the datasnapshot:
Query query = ref.child("time_stamps").orderByKey();
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot item: dataSnapshot.getChildren()){
if (!dataSnapshot.hasChildren()) {
return;
}
for(DataSnapshot childSnapShot :item.getChildren()){
final String timestamp = childSnapShot.getKey();
final DataSnapshot parentSnapShot = childSnapShot.child(timestamp);
if (!parentSnapShot.hasChildren()) {
continue;
}
Double latitude = parentSnapShot.child("latitude").getValue(Double.class);
Double longitude = parentSnapShot.child("longitude").getValue(Double.class);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
etc.
You have:
time_stamp
1510768441759
id: f454445c6e880df31e
coordinates
latitude=-4.1461734
longitude=20.5992437
try this, assuming you do not have this 1510768441759 :
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("time_stamp");
Query query = reference.orderByChild("id").equalTo(f454445c6e880df31e );
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
for(DataSnaphot ds :datas.getChildren()){
String latitudes=ds.child("latitude").getValue().toString();
String longitudes=ds.child("longitudes").getValue().toString();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The above is done assuming you are on this datasnasphot time_stamp. First you do a query to be able to get the coordinates of this id: f454445c6e880df31e. Then since you are on this datasnasphot time_stamp, you have to loop once to be able to retrieve this 1510768441759, then loop again to be able to get the latitude and the longitude.
I am new to Firebase. I have following data in firebase:
Inside EventPlayer I have array of data for key eventID. I want to get all objects for a eventID in an Array.
My code:
final String eventId=intent.getStringExtra("EventID");
mref.child("EventPlayer").child(eventId).orderByChild("eventID").equalTo(eventId);
// Attach a listener to read the data at our posts reference
mref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
EventRequest post = child.getValue(EventRequest.class);
if(post != null){
System.out.println(post);
arrPlayers.add(post);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
The post has no value and it is an empty class.
Edit
Code for creating arrraylist:
arrPlayers = new ArrayList<EventRequest>();
Try this code. The only change is in the first few lines, with the postQuery variable.
final String eventId=intent.getStringExtra("EventID");
Query postQuery = mref.child("EventPlayer").child(eventId).orderByChild("eventID").equalTo(eventId);
// Attach a listener to read the data at our posts reference
postQuery.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
EventRequest post = child.getValue(EventRequest.class);
if(post != null){
System.out.println(post);
arrPlayers.add(post);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
Your second line of code creates a Query object, but you never assign it to anything. Assigning it to a new variable and then adding the ValueEventListener to that new variable should get you to your data.
To get all those event ids, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference eventIdRef = rootRef.child("EventPlayer").child(eventId);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String eventID = ds.child("eventID").getValue(String.class);
list.add(eventID);
Log.d("TAG", eventID);
}
Log.d("TAG", list);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
eventIdRef.addListenerForSingleValueEvent(eventListener);