我正在尝试进行一个活动,该活动显示类并在recyclerview中列出它们。但是,我在从Firebase查询数据时遇到问题。在显示数据库结构后,我将进一步解释。这是我的数据库结构:
"Classes": {
"-Li9nBUgnFmCWqUmwV5W": {
"class_info": {
"date_clasname": "August 6, 2019",
"room_number": "131",
"subject": "Science",
"teacher": "Ms.Henry",
"id": "-Li9nBUgnFmCWqUmwV5W"
}
},
"-Li9n_IYwbTNPdNAsTAu": {
"class_info": {
"date_clasname": "August 6, 2019",
"room_number": "131",
"subject": "Math",
"teacher": "Ms.Henry",
"id": "-Li9n_IYwbTNPdNAsTAu"
}
},
"-Li9naUipsa865NBpZjW": {
"class_info": {
"date_clasname": "August 6, 2019",
"room_number": "131",
"subject": "Other",
"teacher": "Ms.Henry",
"id": "-Li9naUipsa865NBpZjW"
}
},
"-Li9nbpwp2qchwmZRDP9": {
"class_info": {
"date_clasname": "August 6, 2019",
"room_number": "131",
"subject": "Technology",
"teacher": "Ms.Henry",
"id": "-Li9nbpwp2qchwmZRDP9"
}
},
"-Li9ne0NDyjcB4SIWi1z": {
"class_info": {
"date_clasname": "August 6, 2019",
"room_number": "131",
"subject": "Social Studies",
"teacher": "Ms.Henry",
"id": "-Li9ne0NDyjcB4SIWi1z"
}
}
},
我正在尝试按主题查询类,并且仅显示主题为“科学”的类。我已经尝试了多种方法,但是仍然不知道该怎么做。这是我的活动代码:
public class Science_classes extends AppCompatActivity {
private FirebaseDatabase database;
private DatabaseReference myRef, newmf;
private List<Listdata> list;
private RecyclerView recyclerview;
String class_value = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science_classes);
recyclerview = (RecyclerView) findViewById(R.id.rview);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Classes");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String id = dataSnapshot.getKey();
Query query = myRef.child(id).child("class_info").orderByChild("subject").equalTo("Science");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list = new ArrayList<>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Class_model new_class = dataSnapshot1.getValue(Class_model.class);
String nameofclass = new_class.getDate_clasname();
String teacherofclass = new_class.getTeacher();
String roomnumberofclass = new_class.getRoom_number();
String class_key = new_class.getUid();
Listdata listdata = new Listdata(nameofclass, teacherofclass, roomnumberofclass, class_key);
//String name = userdetails.getName();
//String email = userdetails.getEmail();
//String address = userdetails.getAddress();
listdata.setDate_class(nameofclass);
listdata.setTeacher(teacherofclass);
listdata.setRnumber(roomnumberofclass);
list.add(listdata);
}
RecyclerviewAdapter2 recycler = new RecyclerviewAdapter2(list);
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(Science_classes.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
我认为我的问题可能是我无法获得随机生成的ID,并将其设置为onChildAdded中另一个孩子“ class_info”之前的孩子。 谢谢!
答案 0 :(得分:2)
您正在构建如下查询:
Query query = myRef.child(id).child("class_info").orderByChild("subject").equalTo("Science");
这表示通过id
(我认为未在您的JSON中显示)找到一个孩子,然后找到一个孩子class_info
,然后在该孩子下面找到每个孩子的subject
属性子并对此进行过滤。因此路径为$id/class_info/*/subject
,其中*
是要过滤并返回的子节点。
但是您实际拥有的是/Classes/*/class_info/subject
。因此,您有一个节点Classes
,您想返回其节点class_info/subject
具有给定值的子节点。在代码中是:
DatabaseReference classesRef = FirebaseDatabase.getInstance().getReference("Classes");
Query query = classesRef.orderByChild("class_info/subject").equalTo("Science");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list = new ArrayList<>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Class_model new_class = dataSnapshot1.getValue(Class_model.class);
...