如何使用微调器从 Firebase 检索列表?
How to Retrieve a List from Firebase using a spinner?
所以我试图根据扇区微调器检索所有项目的列表,因为有些项目属于不同的扇区,我想显示属于特定扇区的案例
我有两个 类 一个存储状态和日期,另一个存储位置
地点 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";
}
案例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";
}
}
向案例添加位置 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";
}
}
打开 Firestore 并启动一个项目。
在工具 > Firebase > Firestore 中将其连接到 Android Studio。
使用 String 表示状态,Timestamp 表示日期,Map 表示位置
将此代码添加到您的项目中。
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"
}
}
}
});
如何将案例添加到 Firestore 集合:
Case case = new Case(new Date(), "positive", new Location("somesector", "somearea", 150d, 555d));
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("cases").add(case);
所以我试图根据扇区微调器检索所有项目的列表,因为有些项目属于不同的扇区,我想显示属于特定扇区的案例
我有两个 类 一个存储状态和日期,另一个存储位置 地点 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";
}
案例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";
}
}
向案例添加位置 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";
}
}
打开 Firestore 并启动一个项目。 在工具 > Firebase > Firestore 中将其连接到 Android Studio。 使用 String 表示状态,Timestamp 表示日期,Map 表示位置
将此代码添加到您的项目中。
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"
}
}
}
});
如何将案例添加到 Firestore 集合:
Case case = new Case(new Date(), "positive", new Location("somesector", "somearea", 150d, 555d));
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("cases").add(case);