I am fetching data from Firebase
and after fetching the supposed data, I stored it on an ArrayList
. So here is the sample code.
markerArray = new ArrayList<>();
Firebase ref = new Firebase(Config.FIREBASE_URL_DRIVER);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() == 0) {
markerInfo();
} else {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
name = snapshot.child("driversName").getValue().toString().trim();
busNum = snapshot.child("busNum").getValue().toString().trim();
latitude = Double.valueOf(snapshot.child("latitude").getValue().toString().trim());
longitude = Double.valueOf(snapshot.child("longitude").getValue().toString().trim());
availableSeat = snapshot.child("availableSeat").getValue().toString().trim();
estimatedTime = snapshot.child("estimatedTime").getValue().toString().trim();
if ((!latitude.equals(null) || latitude.equals(0)) && (!longitude.equals(null) || longitude.equals(0)) && availableSeat.equals("") && (!estimatedTime.equals("") || estimatedTime.equals("0"))) {
convertLatLong();
getTotalPass();
Toast.makeText(MainMapActivity.this, currentLocation+" :currentLocation", Toast.LENGTH_SHORT).show();
markerArray.add(new Driver(name, totalPassenger, busNum, latitude, longitude, currentLocation, estimatedTime));
}
}
Toast.makeText(MainMapActivity.this, "markerArraySize: "+markerArray.size(), Toast.LENGTH_SHORT).show();
for (i = 0; i < markerArray.size(); i++) {
createMarker(markerArray.get(i).getDriversName(), markerArray.get(i).getTotalPassenger(), markerArray.get(i).getBusNum(), markerArray.get(i).getLatitude(), markerArray.get(i).getLongitude(), markerArray .get(i).getLocation(), markerArray.get(i).getEstimatedTime());
}
}
}
}
I want to update my ArrayList
so that whenever there are changes on the Firebase (especially the latitude and longitude) being fetched, the markers will not be redundant/multiply.