ListView 显示来自 Firebase 问题的所有数据
Posted
技术标签:
【中文标题】ListView 显示来自 Firebase 问题的所有数据【英文标题】:ListView Displays all of the data from Firebase issue 【发布时间】:2021-02-27 17:22:07 【问题描述】:我已经创建了一个旋转器,包含一个扇区列表,并且选择一个应该显示属于该特定扇区的项目,我认为我的查询有一个问题,从Firebase实时数据库获取数据返回列表中的所有项目
关于创建代码
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);
位置类
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";
案例分类
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 来做,但我必须实时做,我认为查询是错误的
【问题讨论】:
【参考方案1】:orderByChild
和equalTo
等查询构建方法返回一个新的Query
对象,因此您必须更新query
变量以引用该新对象。
Query query = databaseReference.child("/Cases");
query = query.orderByChild("/location/sector").equalTo(sector);
query.addValueEventListener(new ValueEventListener()
【讨论】:
感谢指出,原来是这个问题【参考方案2】:改变这个:
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)
);
【讨论】:
以上是关于ListView 显示来自 Firebase 问题的所有数据的主要内容,如果未能解决你的问题,请参考以下文章