简体   繁体   English

如何使用微调器从 Firebase 中检索列表?

[英]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所以我试图根据部门微调器检索所有项目的列表,因为有些项目属于不同的部门,我想显示属于特定部门的案例

树视图

I have two classes one that stores the status and date the other stores the location Location Class我有两个类,一个存储状态和日期,另一个存储位置 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案例 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";

    }
}

Add Location to the Case class. Also you should make variables private.将位置添加到案例 class。您还应该将变量设为私有。

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.打开 Firestore 并启动一个项目。 Connect it to Android Studio in Tools > Firebase > Firestore.在工具 > Firebase > Firestore 中将其连接到 Android Studio。 Use String for status, Timestamp for date and Map for location使用 String 作为状态,Timestamp 作为日期,Map 作为位置

在你的情况下它应该是什么样子

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:如何将案例添加到 Firestore 集合:

Case case = new Case(new Date(), "positive", new Location("somesector", "somearea", 150d, 555d));

FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("cases").add(case);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM