gpt4 book ai didi

java - Android Firebase 查询多个值

转载 作者:行者123 更新时间:2023-12-01 17:39:02 26 4
gpt4 key购买 nike

我想获取类别为“abc”的医生的所有 uid(示例),然后将这些检索到的 uid 存储到数组列表中。我想要与以下 sql 查询显示的结果相同的结果:
从医生中选择uid,其中类别=“abc”;

structure

下一个 Activity 的代码写在下面

private void loadDoctors() {

ArrayList<String> uid = getIntent().getStringArrayListExtra("doctor_list");

for (int i =0;i<uid.size();i++){
DatabaseReference doctors = FirebaseDatabase.getInstance().getReference("Doctors").child(uid.get(i));
Adapter = new FirebaseRecyclerAdapter<DoctorModel, DoctorViewHolder>(
DoctorModel.class,
R.layout.doctors_home,
DoctorViewHolder.class,
doctors
) {
@Override
protected void populateViewHolder(final DoctorViewHolder viewHolder, final DoctorModel model, int position) {
viewHolder.doctor_name.setText(model.getName());

Glide.with(Doctors.this).load(model.getProfileimage()).into(viewHolder.doctor_image);

viewHolder.qualification.setText(model.getQualification());

viewHolder.rating.setText(model.getRating());

}

};
Adapter.notifyDataSetChanged();
doctors_list.setAdapter(Adapter);
}

最佳答案

要解决此问题,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference doctorsRef = rootRef.child("Doctors");
Query categoryQuery = doctorsRef.orderByChild("category").equalTo("Cardiology");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> uidList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String uid = ds.getKey();
uidList.add(uid);
}

//Do what you need to do with your list
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
categoryQuery.addListenerForSingleValueEvent(valueEventListener);

最后,您将得到一个包含 uid 的列表。还有一件事需要注意,因为 Firebease API 是异步的,所以您只能在 onDataChange() 方法中使用该列表。

关于java - Android Firebase 查询多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60994601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com