ListView 显示来自 Firebase 问题的所有数据

ListView Displays all of the data from Firebase issue

我创建了一个旋转器,该旋转器包含一个扇区列表,并且在选择该扇区时,它应该显示属于该特定部门的项目,我认为我的查询存在一个问题,可以从Firebase实时数据库中获取数据,因为它 returns 列表中的所有项目

关于创建代码

 FirebaseApp.initializeApp(this);
    listView = findViewById(R.id.listView);

    spnSector = findViewById(R.id.spnSector);

    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
            R.array.sector_array, android.R.layout.simple_spinner_item);

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spnSector.setAdapter(adapter1);

    spnSector.setOnItemSelectedListener(this);

地点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 {

    private String caseName;
    private String date;
    private String caseStatus;
    private Location location;

    public String getDate() {
        return date;
    }

    public String getCaseName() {
        return caseName;
    }

    public String getCaseStatus() {
        return caseStatus;
    }

    public Location getLocation() {
        return location;
    }

    public Case() {
    }


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

    @Override
    public String toString() {
        return "\nCase Date=" + date + "\n" +
                "Area=" + location.area + "\n" +
                "Latitude=" + location.latitude + "\n" +
                "Longitude=" + location.longitude + "\n"+
                "Sector=" + location.sector + "\n" +
                "Status=" + caseStatus + "\n" +
                "Case name=" + caseName + "\n" ;


    }
}

搜索查询方式

 public void doSearch3(View view) {

    String sector = spnSector.getSelectedItem().toString();

    final ArrayList<Case> cases = new ArrayList<>();


    final ArrayAdapter adapter = new ArrayAdapter<> (SearchCase.this, android.R.layout.simple_list_item_1, cases);

    databaseReference = FirebaseDatabase.getInstance().getReference();

    Query query = databaseReference.child("/Cases");
    query.orderByChild("/location/sector").equalTo(sector);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()){

                Case case1 = ds.getValue(Case.class);
                cases.add(case1);

            }
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });



}

我可以用 firestore 完成,但我必须实时完成,我认为查询是错误的

查询orderByChildequalTo等构建方法 return一个新的Query对象,所以要更新query变量来引用那个新对象。

Query query = databaseReference.child("/Cases");
query = query.orderByChild("/location/sector").equalTo(sector);
query.addValueEventListener(new ValueEventListener() {

改变这个:

databaseReference = FirebaseDatabase.getInstance().getReference();

    Query query = databaseReference.child("/Cases");
    query.orderByChild("/location/sector").equalTo(sector);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()){

                Case case1 = ds.getValue(Case.class);
                cases.add(case1);

            }
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

对此:

databaseReference = FirebaseDatabase.getInstance().getReference();

    Query query = databaseReference.child("/Cases");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()){
             if (ds.child("location").child("sector").getValue(String.class).equals(sector)){
                Case case1 = ds.getValue(Case.class);
                cases.add(case1);
   }

                

            }
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });