5

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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
qqrrrr
  • 575
  • 2
  • 8
  • 22

4 Answers4

5

So what did I understand from your code is that, you want to uniquely identify the changes of the location of each driver you have there in your application.

So yes, keeping a HashMap is a nice idea as you won't have to be worried about the unique key that you're using to identify the location change of a specific driver.

But hence, I would like to suggest to keep the data fetch from your Firebase in a local sqlite database file and each time it gets updated, you can re-analyze your data to create the markers again (i.e. refreshing the markers).

For example, you can have a three tables.

  • One for listing all the drivers (Primary key driver_id)
  • The trips you're having. (Primary key trip_id and each row in the Trip table is associated with a driver_id)
  • One for listing all the rides (this table will have all the information about each ride along with the latitude and longitude. Each update in your firebase database will have an entry here)

The tables may look like

Drivers table

driver_id - 1
name - Driver 1

driver_id - 2
name - Driver 2

Trips table

trip_id - 1
driver_id - 1

trip_id - 2 
driver_id - 1

trip_id - 3 
driver_id - 2

Rides table

trip_id - 1
driver_id - 1 
longitude - 9.4
latitude - 38
updateTime - 78783232

trip_id - 1
driver_id - 1 
longitude - 5.8
latitude - 53
updateTime - 78783242

trip_id - 1
driver_id - 1 
longitude - 78
latitude - 56
updateTime - 78783252

trip_id - 2
driver_id - 1 
longitude - 8
latitude - 77
updateTime - 78783262

Now from the Rides table you re-analyze your data each time Firebase updates the local database and redraw the markers for each driver in the map.

You can have simple sql queries to make a path the driver has travelled based on the updateTime. Sort the path points based on updateTime and remove any consecutive data which have the same longitude and latitude.

Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
2

using set(position,data) method of arraylist to consume your demand.

 ArrayList<String> alstr = new ArrayList<>();
            alstr.add("khan");
            alstr.add("yogesh");
            alstr.add("kapil");
            alstr.add("rajoria");

            for(String str : alstr) {
                System.out.println(str);
            }
            // update value here
            alstr.set(3, "Ramveer");
            System.out.println("with Iterator");
            Iterator<String> itr = alstr.iterator();

            while (itr.hasNext()) {
                Object obj = itr.next();
                System.out.println(obj);

            }
Keyur Thumar
  • 608
  • 7
  • 19
  • See about code. If you don't know position of item in list. First create common tag(like ID in pojo class). Than search for that id and than replace your data. – Keyur Thumar Sep 29 '16 at 09:07
1

I'd suggest using a map (HashMap e.g.) where you save your data with ids.

Then later on when a change happens you can search for the id, whether it is already stored in the map, and change the value for the corresponding key.

Also you can get all the values from a map as a list, if you use it as a list elsewhere.

Some help with maps: Driver should be your type, String can be the type of the key like the busNum or the name. Creating the map:

Map<String, Driver> markersMap = new HashMap<String, Driver>();

Putting values with checking they key:

Driver driver = new Driver(name, totalPassenger, busNum, latitude, longitude, currentLocation, estimatedTime);
// that's how you check if there's already a value
markersMap.containsKey(name); 
// A put overwrites the value anyway, so maybe you don't need the check
markersMap.put(name, driver)

Also for getting the values as a list there should be a values() method of map, or something like that.

abbath
  • 2,444
  • 4
  • 28
  • 40
  • but how to deal with `HashMap`. Can you give some snippets? – qqrrrr Sep 29 '16 at 08:13
  • Well, HashMap is a very very common and basic programming concept, so you should find a lot of sources regarding it. Find some ideas [_here_](https://www.firebase.com/docs/android/guide/saving-data.html), I'll try to add some snippet, however I haven't used firebase yet. – abbath Sep 29 '16 at 08:17
  • ah okay! thank you. I haven't tried yet. I'll feedback you later :) – qqrrrr Sep 29 '16 at 08:46
0

You can use set() method of ArrayList class with position and data argument to set the value in ArrayList.

set(int index, Element E)

but this will help you out only if you know the position of item need to be change.