how do I display all markers from the firebase database?
I have come into the database the data in this form:
but only what is highlighted in the drawing is displayed! I write the code like this:
#Override
public void onMapReady(GoogleMap googleMap) {
if (!isOnline() && !isNetworkAvailable()) {
} else {
Map1 = googleMap;
Map1.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Map1.setMapStyle(mapStyleOptions);
Map1.setOnMyLocationButtonClickListener(this);
Map1.setOnMyLocationClickListener(this);
if (Map1 == null) {
Map1.setMyLocationEnabled(true);
}
startCurrentLocationUpdates();
mHandler.postDelayed(new Runnable(){
public void run(){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Markers");
Map.clear();
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()) {
String latitude = ds.child("latitude").getValue().toString();
String longitude = ds.child("longitude").getValue().toString();
String bus = ds.child("bus_station").getValue().toString();
Log.d("TAG", latitude + ", " + longitude);
LatLng location = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
Map.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.img_4662878)).position(location).title(bus));
}}}}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
mHandler.postDelayed(this, delay);
}
}, delay);
}}
tell me how to display all markers from the database correctly?
error:
2020-04-22 23:10:12.031 18972-18972/by.sviter.stop E/AndroidRuntime: FATAL EXCEPTION: main
Process: by.sviter.stop, PID: 18972
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at by.sviter.stoprevizor.tabs.maps.MapsFragment$3$1.onDataChange(MapsFragment.java:337)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database##19.2.1:179)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database##19.2.1:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database##19.2.1:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database##19.2.1:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Assuming that dn4O ... mgG2 is the uid of the authenticated user, to get the values of the latitude and longitude properties, please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Markers").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String latitude = dataSnapshot.child("latitude").getValue(String.class);
String longitude = dataSnapshot.child("longitude").getValue(String.class);
Log.d("TAG", latitude + ", " + longitude);
LatLng location = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
//Do what you need to do with the location
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logat will be:
53.9148950..., 27.4380507...
Edit:
According to your comment:
how to show all the tags?
Please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference markersRef = rootRef.child("Markers");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String latitude = ds.child("latitude").getValue(String.class);
String longitude = ds.child("longitude").getValue(String.class);
Log.d("TAG", latitude + ", " + longitude);
LatLng location = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
//Add the location to a list of locations
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
markersRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
// ...
// ...
53.9148950..., 27.4380507...
After the scope of DataSnapshot for loop has ended you can manipulate the contents of the DataModel object using the getters and setters which you should have implemented.
If this does not work. Check if you mMarkers1 object is pointing to the correct class, or change the addListenerForSingleValueEvent to addValueEventListener.
You should you use addValueEventListner instead of addListenerForSingleValueEvent
You must run a for loop to get all data like...
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("Markers").addValueEventListner(new ValueEventListner{
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snap : dataSnapshot.getChildern()){
cast into your model class
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Related
It does not fetch the required value and writes null What is wrong with the code please help
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("analysis_Client").child(searchUserId);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
userTotal = ds.child("Total").getValue(String.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
A picture of the firebase of the way to list the data I want to call both(Total - Rest - ProductNum)
onCreate() method is never executed. I just want to take the below data to 4 TextViews. "Detail" is the model class.
No errors are shown when running the app.
view_temp is an activity.
this is firebase realtime db
this is the java class
`public class view_temp extends AppCompatActivity {
private view_temp viewTemp;
public Detail detail;
private TextView roomtemp, roomhuminity, bodypulse, bodytemp;
private DatabaseReference mDatabase;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_temp);
mDatabase = FirebaseDatabase.getInstance().getReference("heat-stroke-device");
roomtemp = (TextView) findViewById(R.id.txt_room_temp);
roomhuminity = (TextView) findViewById(R.id.txt_huminity_temp);
bodypulse = (TextView) findViewById(R.id.txt_body_pulse);
bodytemp = (TextView) findViewById(R.id.txt_body_temp);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
detail = postSnapshot.getValue(Detail.class);
String rtemp = detail.getRoomtemp();
String rhumidity = detail.getRoomhumidity();
String bpulse = detail.getBodypulse();
String btemp = detail.getBodytemp();
roomtemp.setText(rtemp);
roomhuminity.setText(rhumidity);
bodypulse.setText(bpulse);
bodytemp.setText(btemp);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
}
}`
There is no need to add the name of the project in the getReference() method. To get those names and the corresponding values correctly, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.getKey();
double value = ds.getValue(Double.class);
Log.d("TAG", name "/" + value);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
rootRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
RoomHumi/86.0
RoomTemp/30.8
bodyPulse/126.0
bodyTemp/29.0
If the keys are always fixed, then simply use:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
double roomHumi = dataSnapshot.child("RoomHumi").getValue(Double.class);
Log.d("TAG", "RoomHumi" "/" + valueroomHumi);
double roomTemp = dataSnapshot.child("RoomTemp").getValue(Double.class);
Log.d("TAG", "RoomTemp" "/" + roomTemp);
double bodyPulse = dataSnapshot.child("bodyPulse").getValue(Double.class);
Log.d("TAG", "bodyPulse" "/" + bodyPulse);
double bodyTemp = dataSnapshot.child("bodyTemp").getValue(Double.class);
Log.d("TAG", "bodyTemp" "/" + bodyTemp);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
rootRef.addListenerForSingleValueEvent(valueEventListener);
And you'll have the same result in the logcat.
Just change your line
mDatabase = FirebaseDatabase.getInstance().getReference("heat-stroke-device");
to
mDatabase = FirebaseDatabase.getInstance().getReference();
Note: You do not need to put your database name into reference.
My database looks as follows, in here I want to retrieve the data inside the mylocation child (see the attached image here). But it is not working.
How to solve that?
My current code looks as this,
private void loadAddress() {
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Users");
ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
String name=""+ds.child("name").getValue();
String lat=""+ds.child("name2").getValue();
addresstv.setText(name);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
To get the data under mylocation, you use an explicit call to that child, like in the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("mylocation").child("name2").getValue(String.class);
double latitude = dataSnapshot.child("mylocation").child("latitude").getValue(Double.class);
double longitude = dataSnapshot.child("mylocation").child("longitude").getValue(Double.class);
Log.d("TAG", name + "/" + latitude + "/" + longitude);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
5555/79.8571577/6.9448882
As shown in the picture I have campaign branch which is a dynamic list where I save every campaigns data.
https://drive.google.com/file/d/16xe1aeAxEbtfiiUFoXtlqvFPucHejQp7/view?usp=sharing
Every campaign data includes location which is an other branch inside the campaign,
I want to get location data of all campaigns.
I tried it using the code below but it return null value.
void getCampaignsLocations(final OnGetDataListener onGetDataListener){
onGetDataListener.onStart();
Toast.makeText(this, "started", Toast.LENGTH_SHORT).show();
DatabaseReference campaignsRoot = FirebaseDatabase.getInstance().getReference();
DatabaseReference campaignsPath = campaignsRoot.child("campaigns");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
LatLng l = ds.child("location").getValue(LatLng.class);
campaignsLocations.add(l);
onGetDataListener.onSuccess(dataSnapshot,0,"");
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
campaignsPath.addListenerForSingleValueEvent(valueEventListener);
}
To solve this, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference campaignsRef = rootRef.child("campaigns");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
double latitude = ds.child("location").child("latitude").getValue(Double.class);
double longitute = ds.child("location").child("longitute").getValue(Double.class);
Log.d(TAG, latitude ", " + longitute);
LatLng latLng = new LatLng(latitude, longitute);
//Do what you need to do with your LatLng object
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
campaignsRef.addListenerForSingleValueEvent(valueEventListener);
Please note, that in order to create a LatLng object, you need to loop throught the DataSnapshot object to get the latitude and longitute. There is no way you can map the children that exist under the location node to a LatLng object. Beside that, LatLng is not a Firebase supported data type.
I'm trying to retrieve a specific value from a Firebase realtime Database.
The data looks like this:
I want to get the URL value from any of them and then put it into a string variable to use later. This is so that the program does not need to be changed if the download link is changed. The code I have so far is;
DatabaseReference fdb = FirebaseDatabase.getInstance().getReference();
//Query query = fdb.child("URLDOWNLOADS").child("WSSeasons")
fdb.addListenerForSingleValueEvent(new ValueEventListener()
{
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
for (DataSnapshot url : dataSnapshot.getChildren())
{
LoginInfoFirebasedb DownURL = url.getValue(LoginInfoFirebasedb.class);
String temp = DownURL.dwldURL;
URL1 = temp;
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError)
{
}
});
Some help would be seriously appreciated. I understand this may be a simple problem but I am stumped.
To get each url as a String, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("URLDOWNLOADS");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
String url = ds.child("URL").getValue(String.class);
Log.d(TAG, key + " / " + url);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
ref.addListenerForSingleValueEvent(valueEventListener);