Reputation: 33
Currently I am doing a project which require the application to get the real time data from the Realtime database. I have use the datasnapshot method to get the data from database in Tracking activity and send all the data to the Map activity to display the data through intent method.
Here is how I retrieve data from database in Tracking Activity.
private void isTrackingID() {
String userTrackingID = enterTracking.getText().toString().trim();
//Long userTrackingIDNumber = Long.parseLong(userTrackingID);
Toast.makeText(this, "Tracking ID = " + userTrackingID, Toast.LENGTH_SHORT).show();
//DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("HostID");
//DatabaseReference trackingRef = rootRef.child("tracking");
//Query query = trackingRef.orderByChild("id").equalTo(userTrackingID);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("HostID");
Query query = reference.orderByChild("Reference/ID").equalTo(userTrackingID);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.i("checking", "this is value message");
enterTracking.setError(null);
//String IDfromDatabase = dataSnapshot.child(userTrackingID).getValue(String.class);
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
latitudefromDatabase = String.valueOf(dataSnapshot1.child("HostLocation").child("latitude").getValue());
Log.i("testing for latitude", latitudefromDatabase);
longitudefromDatabase = String.valueOf(dataSnapshot1.child("HostLocation").child("longitude").getValue());
Log.i("testing for longitude", longitudefromDatabase);
String customerAddress = String.valueOf(dataSnapshot1.child("TrackingID").child(userTrackingID).child("customerAddress").getValue());
String customerReplyStatus = String.valueOf(dataSnapshot1.child("TrackingID").child(userTrackingID).child("customerReplyStatus").getValue());
String id = String.valueOf(dataSnapshot1.child("TrackingID").child(userTrackingID).child("id").getValue());
String latitude = String.valueOf(dataSnapshot1.child("TrackingID").child(userTrackingID).child("latitude").getValue());
String longitude = String.valueOf(dataSnapshot1.child("TrackingID").child(userTrackingID).child("longitude").getValue());
String status = String.valueOf(dataSnapshot1.child("TrackingID").child(userTrackingID).child("status").getValue());
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
intent.putExtra("latitude", latitudefromDatabase);
intent.putExtra("longitude", longitudefromDatabase);
intent.putExtra("customerAddress",customerAddress);
intent.putExtra("customerReplyStatus",customerReplyStatus);
intent.putExtra("id",id);
intent.putExtra("latitude",latitude);
intent.putExtra("longitude",longitude);
intent.putExtra("status",status);
startActivity(intent);
}
}else {
enterTracking.setError("Invalid Tracking ID from database");
enterTracking.requestFocus();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("error", databaseError.getMessage());
}
});
}
Now the problem is, when there is data change in the database (for example the host location), the program cannot show the latest data on the Map activity. I need to go back to the Tracking activity page and go to the Map activity again in order to get the latest data. Hence when the host location in the realtime database had been updated, the program cannot show the data in real time. Guys, how do I need to do the correction in order to get the real time data from the database?
Upvotes: 0
Views: 492
Reputation: 600131
To get realtime updates from the database, change this line:
query.addListenerForSingleValueEvent(new ValueEventListener() {
To this:
query.addValueEventListener(new ValueEventListener() {
With this change, you now attached a permanent listener, and your onDataChange
will also be called when there is a change to any data matched by the query.
Upvotes: 1