Ahmad
Ahmad

Reputation: 47

Save data in Model Class or a list from Firebase

I want to get Latitude and longitude and data of all devices like 6430046874, 6430046875, 6430046876.

enter image description here

 new GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);


    FirebaseDatabase database = FirebaseDatabase.getInstance();

    myRef = database.getReference("GPSLocationHistory/DevicesLatest/6430046874");


    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Map<String, Object> objectHashMap = dataSnapshot.getValue(objectsGTypeInd);
            ArrayList<Object> objectArrayList = new ArrayList<Object>(objectHashMap.values());
            System.out.println("value 0= " + objectArrayList.get(0) + " value 1= " + objectArrayList.get(1) + "value 2= " + objectArrayList.get(2) + " value 3= " + objectArrayList.get(3) + "value 4= " + objectArrayList.get(4) + " value 5= " + objectArrayList.get(5) + " value 6= " + objectArrayList.get(6));
            // LatLng location = new LatLng(latitude, longitude);
            String latLng = objectArrayList.get(2).toString() + "," + objectArrayList.get(3).toString();
            System.out.print("oyy " + latLng);
            Toast.makeText(MapsActivity.this,""+latLng.toString(),Toast.LENGTH_SHORT).show();

             latlongFcm = new LatLng(Double.parseDouble(objectArrayList.get(2).toString()), Double.parseDouble(objectArrayList.get(3).toString()));
           mMap.clear();
            Marker marker;
             marker = mMap.addMarker(new MarkerOptions()
                    .position(latlongFcm)
                    .title("San Francisco")
                    .snippet("Population: 776733"));


            for (DataSnapshot alert : dataSnapshot.getChildren()) {
                System.out.println(alert.getValue());
            }


        }

This code will show only data of one User like 6430046874

i want to make Model class and get data from here of all the user and plot the markers.

Upvotes: 1

Views: 2785

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

You get only one because you did not iterate over the entire object. So in order to solve this, please use this code:

FirebaseDatabase database = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = database.child("GPSLocationHistory").child("DevicesLatest");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String deviceId = (String) ds.getKey();

            DatabaseReference deviceIdRef = database.child("GPSLocationHistory").child("DevicesLatest").child(deviceId);
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Double lati = (Double) dataSnapshot.child(lati).getValue();
                    Double longi = (Double) dataSnapshot.child(longi).getValue();
                    //and so on for the other values
                    //and do what you want with this values
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException(); // don't ignore errors
                }
            };
            deviceIdRef.addListenerForSingleValueEvent(eventListener);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
};
myRef.addListenerForSingleValueEvent(eventListener);

Upvotes: 0

Ashish P
Ashish P

Reputation: 3056

Step 1) Make a PoJo.class which contains all the keys that are in DeviceLatest child node with similar datatype

Step 2) Go to desired child node here in your scenario it is GpsLocationHistory-DeviceLatest. so here we have to take two time child ..Please correct the spelling of child node

Step 3) Write down below method and call.

public void getAllUsersFromFirebase() {
    DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference().child("GpsLocationHistory").child("DeviceLatest");
    UserRef.keepSynced(true);
    UserRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
            List<PoJo> fcmUsers = new ArrayList<>();
            while (dataSnapshots.hasNext()) {
                DataSnapshot dataSnapshotChild = dataSnapshots.next();
                PoJo fcmUser = dataSnapshotChild.getValue(PoJo.class);
                fcmUsers.add(fcmUser);

            }

            // Check your arraylist size and pass to list view like
            if(fcmUsers.size()>0)
            {
                fcmUsers arraylist pass to list view
            }else
            {
                // Display dialog for there is no user available.
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
                // for handling database error          
        }
    });
}

Upvotes: 4

Related Questions