gpt4 book ai didi

java - 如何对多个 child 执行查询?

转载 作者:行者123 更新时间:2023-11-29 18:30:07 28 4
gpt4 key购买 nike

我正在尝试进行一项 Activity ,以显示类并将它们列在回收站 View 中。但是,我在从 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"
}
}
},

我正在尝试按主题查询类(class),并且只显示主题为“科学”的类(class)。我尝试了各种各样的事情,但仍然不知道该怎么做。这是我的 Activity 代码:

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 中将其设置为我的 child 之前我的另一个 child “class_info”。谢谢!

最佳答案

您正在构建这样的查询:

Query query = myRef.child(id).child("class_info").orderByChild("subject").equalTo("Science");

这是说通过 id 找到一个 child (我认为它没有显示在你的 JSON 中)然后找到一个 child class_info,然后在那个下面找到 subject 每个 child 的属性并对其进行过滤。所以路径是$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);
...

关于java - 如何对多个 child 执行查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56745262/

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