I have a String stationName and I want to fetch distance from FireBase according to the stationName I provided.
Here is my Firebase Database:
I am using orderByChild query to filter result and I need fetch only the distance value. Here is my code:
Query query = FirebaseDatabase.getInstance().getReference("Stations").orderByChild("Name").limitToFirst(1).equalTo(sourceName);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String distance;
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
distance = dataSnapshot.child("Distance").getValue(String.class);
tt.setText(distance); //to check if it is working or not
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I need to know what is wrong with code. It's not returning any value.
Thanks in advance
Replace this
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
distance = dataSnapshot.child("Distance").getValue(String.class);
tt.setText(distance); //to check if it is working or not
}
With this
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
distance = snapshot.child("Distance").getValue().toString(); // you
//cannot pass the String.class as in the getValue() parameters becasue a long value
//can not be cast to class object so you need to call toString() method to convert long value
//into the string value.
tt.setText(distance); //to check if it is working or not
}
Hope it helps
Related
In the picture is my database, so I want to retrieve the value of latitude and longitude from my databasem but the child name is named from random time based on the time when the data uploaded.
How can I get the value of latitude and longitude when I don't know what time that the child used because it's random time?
You want to fetch all data or any specific data?
EDIT:
FirebaseDatabase.getInstance().getReference().child("History").child("Saturday").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
String lat = snapshot.child("Latitude").getValue().toString();
String longi = snapshot.child("Longitude").getValue().toString();
//OR you can typecast the entire snapshot object into your model class
ModelClass model = new ModelClass();
model = snapshot.getValue(ModelClass.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I want to retrive division and rno from for all key in array and compare those values in array with two other strings. how to do that?
You can get all children. try below code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference();
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {
String rno = ds.child("rno").getValue().toString();
String div = ds.child("division").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I don't understand your question. But you can play on those who fulfill the requirements of the child and fulfill the conditions.
DatabaseReference roomRef = dbRef.child("Everybody Room List Messages");
ValueEventListener roomEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dsKey : dataSnapshot.getChildren()) {
for (DataSnapshot dsNick : dsKey.getChildren()) {
if (dsNick.child("nick").getValue().toString().equals(Nick)) {
dsNick.child("image").getRef().setValue("");
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
roomRef.addListenerForSingleValueEvent(roomEventListener);
UPDATE
So, if I understand, you need this code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot item: dataSnapshot.getChildren()) {
String snapDiv = item.child("division").getValue().toString();
String snapRno = item.child("rno").getValue().toString();
if(snapDiv.equals(divi) && snapRno.equals(roll)){
//Here you can do everythings you need because division and rno value are those you are searching.
}
}
}
});
I can't understand completly your question.
You need to retrieve for each child his division and rno value. After that you need to compare them with other variables. But in the session these variables are 2 and fixed or you need to compare with a list of pairs?
After that, assuming that the above question will be clarify, if strings are matches, you need to update the value of sub1, sub2,...,sub5 fields?
Please, give us more informations to understand your situation and give you more support.
My firebase database:
I want to get post id from post element.
I know authorId, createdDate, createdDateText and title, but I want to get post id.
How can I do that?
Please try with firebase query like bellow,
Query query = FirebaseDatabase.getInstance().getReference().child("posts");
query.orderByChild("title").equalTo("test");//Here replace title with your key which you want and replace test with value throw which search
//query.orderByChild("title").equalTo("test").limitToFirst(1); //If you want to only one value
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//you get here the list of post from DB
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String postId = child.getKey(); //Post Id
Logger.print("postId");
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
try final String postId = getRef(position).getKey();
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.
So I have a list of objects stored in firebase and want to query for just one object using child equal to method but I am getting a map with single key value instead.
Code below showing what I want to achieve instead of dealing with hashmaps.
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference coutryRef = database.getReference(FireContract.PATH_COUNTRY);
Query countryQuery = coutryRef.orderByChild(FireContract.CHILD_NAME)
.equalTo(TestUtilities.TEST_COUNTRY_ALGERIA).limitToFirst(1);
countryQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Country country = dataSnapshot.getValue(Country.class);
assertEquals(TestUtilities.TEST_COUNTRY_ALGERIA, country.getName());
//get country key
String coutryKey = dataSnapshot.getKey();
}
#Override
public void onCancelled(DatabaseError databaseError) {
fail("Failed to get country: " + databaseError.getMessage());
}
});
When you fire a query, you will always get a list of results. Even when there is only a single item matching the query, the result will be a list of one item.
To handle the list, you can either use a ChildEvenListener or you can loop over the children in your ValueEventListener:
countryQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot countrySnapshot: dataSnapshot.getChildren()) {
Country country = countrySnapshot.getValue(Country.class);
assertEquals(TestUtilities.TEST_COUNTRY_ALGERIA, country.getName());
//get country key
String countryKey = countrySnapshot.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
fail("Failed to get country: " + databaseError.getMessage());
}
});