Search code examples
androidfirebasefirebase-realtime-databasespinner

How to Retrieve a List from Firebase using a spinner?


So I am trying to retrieve a list of all the items based on the sector spinner, because some items belong to different sectors, and I want to display the cases that belong in a specific sector

Tree View

I have two classes one that stores the status and date the other stores the location Location Class

public class Location {

    public String sector;
    public String area;

    public double longitude;
    public double latitude;

    public String getSector() {
        return sector;
    }

    public String getArea() {
        return area;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public Location()
    {

    }

    public Location(String sector , String area, double longitude, double latitude)
    {
        this. sector = sector;
        this. area = area;
        this.longitude = longitude;
        this.latitude = latitude;
    }

    @Override
    public String toString() {
        return "\n Sector='" + sector + '\'' +
                ",Area Name='" + area + '\'' +
                ",Longitude='" + longitude + '\'' +
                ",Latitude=" + latitude + "\n";

    }

Case Class

public class Case {

    public String date;
    public String caseStatus;

    public String getDate() {
        return date;
    }

    public String getCaseStatus() {
        return caseStatus;
    }

    public Case()
    {

    }

    public Case(String date,String caseStatus)
    {
        this.date = date;
        this.caseStatus = caseStatus;
    }

    @Override
    public String toString() {
        return "\nCase Date='" + date + '\'' +
                ",Case Status='" + caseStatus + "\n";

    }
}

Solution

  • Add Location to the Case class. Also you should make variables private.

    public class Case {
    
        private Date date;
        private String status;
        private Location location;
    
        public Date getDate() {
            return date;
        }
    
        public String getStatus() {
            return status;
        }
    
        public Location getLocation() {
            return location;
        }
    
        public Case() {
        }
    
        public Case(Date date, String status, Location location) {
            this.date = date;
            this.status = status;
            this.location = location;
        }
    
        @Override
        public String toString() {
            return "\nCase Date='" + date + '\'' +
                    ",Case Status='" + status + "\n";
    
        }
    }
    

    Open Firestore and start a project. Connect it to Android Studio in Tools > Firebase > Firestore. Use String for status, Timestamp for date and Map for location

    how it should look like in your case

    Add this code to your project.

        ArrayList<Case> cases = new ArrayList<>();
        
        FirebaseFirestore firestore = FirebaseFirestore.getInstance();
        firestore.collection("cases")
        .whereEqualTo("location.sector", "somesector")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Case case = document.toObject(Case.class);
                                cases.add(case);
    // now you have list of Case objects where sector equals "somesector"
                            }
                        }
                    }
                });
    

    How to add Case to Firestore collection:

    Case case = new Case(new Date(), "positive", new Location("somesector", "somearea", 150d, 555d));
    
    FirebaseFirestore firestore = FirebaseFirestore.getInstance();
    firestore.collection("cases").add(case);