I am retrieving values from FirebaseDatabase. Everything was working fine and I was able to retrieve all items in the database until I tried to filter the values with the following conditional line of code (see below as well)
if ((String) singleUser.get("Type") == "Rider")
With this line I don't get anything in my Arrays, even if the Log.i gives me a lot of riders and there are (Driver)s and (Rider)s in the Type of database when I am not filtering the results. I think Firebase database is giving me dirty string back. Is that true? How can I fix it.
private void collectLocationRiders (Map<String,Object> users) {
//iterate through each user, ignoring their UID
for (Map.Entry<String, Object> entry : users.entrySet()){
//Get user map
Map singleUser = (Map) entry.getValue();
if(String.valueOf(MainActivity.userId)!= String.valueOf(entry.getKey()) ) {
riderKeys.add(entry.getKey());
Log.i("UserKey1", String.valueOf(entry.getKey()));
Log.i("UserKey2", String.valueOf(MainActivity.userId));
//Get Type field and append to list
if ((String) singleUser.get("Type") == "Rider") {
userType.add((String) singleUser.get("Type"));
Log.i("Type", (String) singleUser.get("Type"));
//Get Location field and append to list
Map location = (Map) singleUser.get("Location");
if (location != null) {
Number latitude = (Number) location.get("latitude");
Number longitude = (Number) location.get("longitude");
//Set Rider location
riderLoc.setLatitude(latitude.doubleValue());
riderLoc.setLongitude(longitude.doubleValue());
//distances(latitude, longitude, latitude, longitude );
distances.add(distance(riderLoc));
locations.add(new LatLng(latitude.doubleValue(), longitude.doubleValue()));
}
arrayAdapter.notifyDataSetChanged();
}
}
}
The response I am getting per each item when the if conditional is not included is the following:
2020-02-02 13:34:53.187 6923-6923/com.example.uberapp I/Type: Rider
2020-02-02 13:34:53.188 6923-6923/com.example.uberapp I/Type: Driver
2020-02-02 13:34:53.189 6923-6923/com.example.uberapp I/Type: Driver
2020-02-02 13:34:53.189 6923-6923/com.example.uberapp I/Type: Rider
2020-02-02 13:34:53.189 6923-6923/com.example.uberapp I/Type: Rider
2020-02-02 13:34:53.189 6923-6923/com.example.uberapp I/userType: [Rider, Driver, Driver, Rider, Rider]
It means that when I filter the results I should get 3 Riders in my ListView but I don't get anything...
2020-02-02 14:03:43.711 7240-7240/com.example.uberapp I/userType: []