How to update an item stored in an ArrayList? (Android)

I am extracting data from Firebaseand after extracting the alleged data I saved it to ArrayList. So here is a 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 in Firebase (especially in latitude and longitude), the markers will not be redundant / multiple.

+4
source share
4 answers

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

, HashMap - , , , .

, , Firebase sqlite, , , , (, ),

, .

  • ( driver_id)
  • , . ( trip_id, Trip driver_id)
  • ( , . firebase )

driver_id - 1
name - Driver 1

driver_id - 2
name - Driver 2

trip_id - 1
driver_id - 1

trip_id - 2 
driver_id - 1

trip_id - 3 
driver_id - 2

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

Rides , Firebase .

sql-, , updateTime. updateTime , .

, .

+5

map (HashMap ), .

, , , , .

, .

: , String , busNum . :

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

:

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

values() - .

+1

set (position, data), arraylist .

 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);

            }
+1

set() ArrayList position data ArrayList.

set(int index, Element E)

but this will help you only if you know that the position of the element needs to be changed.

0
source

Source: https://habr.com/ru/post/1656186/


All Articles