Reputation: 33
I am new in using Android Studio and Firebase. I am trying to retrieve the latitude and longitude data in the HostID when I using UserID account from Firebase realtime database to search the tracking ID and show the latitude and longitude on the map when the user search with tracking ID.
I have tried to use the query and orderbychild command and the code is as below.
private void isTrackingID() {
String userTrackingID = enterTracking.getText().toString().trim();
Toast.makeText(this, "testing = " + userTrackingID, Toast.LENGTH_SHORT).show();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("HostID").child(firebaseAuth.getUid());
Query query = reference.orderByChild("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()) {
String lol = dataSnapshot1.getKey();
Log.i("testing for lol", lol);
String value = String.valueOf(dataSnapshot1.child("latitude").getValue());
Log.i("testing for latitude", value);
String latitudefromDatabase = dataSnapshot1.child("latitude").getValue(String.class);
String longitudefromDatabase = dataSnapshot1.child("longitude").getValue(String.class);
Intent intent = new Intent(Latitude_Longitude.this, MapsActivity.class);
intent.putExtra("latitude", latitudefromDatabase);
intent.putExtra("longitude", longitudefromDatabase);
startActivity(intent);
}
}else {
enterTracking.setError("Invalid Tracking ID from database");
enterTracking.requestFocus();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("error", databaseError.getMessage());
}
});
}
I keep finding tutorial and and try to apply it to my program. However, I still dont know why and where the coding was wrong as the program cannot shown the message "checking, this is value message" in the logcat when the button is pressed and straight showing the "Invalid Tracking ID from database", which mean the program cannot get the data from database.
Please help me and show me how to correct my coding.
UPDATES!!! I have made some changes to receive the data.
private void isTrackingID() {
String userTrackingID = enterTracking.getText().toString().trim();
Long userTrackingIDNumber = Long.parseLong(userTrackingID);
Toast.makeText(this, "testing = " + userTrackingIDNumber, Toast.LENGTH_SHORT).show();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("HostID");
Query query = reference.orderByChild("TrackingID/ID").equalTo(userTrackingIDNumber);
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()) {
String latitudefromDatabase = String.valueOf(dataSnapshot1.child("latitude").getValue());
Log.i("testing for latitude", latitudefromDatabase);
String longitudefromDatabase = String.valueOf(dataSnapshot1.child("longitude").getValue());
Log.i("testing for longitude", longitudefromDatabase);
//String latitudefromDatabase = dataSnapshot.child("latitude").getValue(String.class);
//Log.i("testing for latitude", latitudefromDatabase);
//String longitudefromDatabase = dataSnapshot.child("longitude").getValue(String.class);
//Log.i("testing for longitude", longitudefromDatabase;
//String latitudefromDatabase = dataSnapshot1.child("latitude").getValue(String.class);
//Log.i("testing for latitude", latitudefromDatabase);
//String longitudefromDatabase = dataSnapshot1.child("longitude").getValue(String.class);
//Log.i("testing for longitude", latitudefromDatabase);
Double Latitude = Double.parseDouble(latitudefromDatabase);
Double Longitude = Double.parseDouble(longitudefromDatabase);
Intent intent = new Intent(Latitude_Longitude.this, MapsActivity.class);
intent.putExtra("latitude", Latitude);
intent.putExtra("longitude", Longitude);
startActivity(intent);
}
}else {
enterTracking.setError("Invalid Tracking ID from database");
enterTracking.requestFocus();
}
}
Now, the error shows that the input string value was null as shown in the diagram below.
So, now how should I do to get rid of this error?
Upvotes: 0
Views: 176
Reputation: 598728
There are at least two problems in your code:
orderByChild("ID")
which means Firebase looks for an ID
property directly under each direct child node of /HostID/$uid
. But the ID
property is actually under TrackingID/ID
, so that's what you should order/filter on.equalTo(userTrackingID
. But the ID
values in your database are stored as numeric values, so you should pass a numeric value in the query too.Combined that means this should be better:
String userTrackingID = enterTracking.getText().toString().trim();
Long userTrackingIDNumber = Long.parseLong(userTrackingID)
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("HostID").child(firebaseAuth.getUid());
Query query = reference.orderByChild("TrackingID/ID").equalTo(userTrackingIDNumber);
Upvotes: 1