[英]How to get child of child value from firebase whose parent value is unknown in java
I want to get data from firebase without changing my database structure because I have two condition:我想在不更改数据库结构的情况下从 firebase 获取数据,因为我有两个条件:
When admin logins then he can see all employees data for the selected year or say year-wise.当管理员登录时,他可以看到所选年份或每年的所有员工数据。
But when employees login then they should be able to see their individual data from all the years using employee code, which is a child also (blue tick in the picture).但是当员工登录时,他们应该能够使用员工代码查看所有年份的个人数据,这也是一个孩子(图中的蓝色勾号)。
Below is my database structure:下面是我的数据库结构:
The child marked in red is unknown in case of employee's access and the blue tick denotes an employee who may be present in every year.在员工访问的情况下,红色标记的孩子是未知的,蓝色勾号表示每年可能出现的员工。
All I want is to achieve the 2nd condition.我只想达到第二个条件。 But I am unable to get the data.但是我无法获取数据。 Here is my code:这是我的代码:
private void retrieveData() {
final String shift = kvName.getText().toString();
final String employeeCode = empCode.getText().toString();
dbRef.child("Apar").child(shift);
dbRef.orderByChild(employeeCode).equalTo(employeeCode).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas1 : dataSnapshot.getChildren()) {
for (DataSnapshot datas2 : datas1.getChildren()) {
// String aparGrading= datas2.getKey(); //unable to figure out how to get
// Toast.makeText(Apar.this, aparGrading, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Your current data structure makes it easy to find the information for all employees for a specific year.您当前的数据结构使查找特定年份所有员工的信息变得容易。 It does however not make it easy to find the information for a specific employee across all years.然而,要找到特定员工所有年份的信息并不容易。
To do the latter, you have two options:要执行后者,您有两种选择:
For more on this, also see:有关更多信息,另请参阅:
After reading the various answers on this topic linked above by Mr. @Frank van Puffelen and spending some times over it, the problem is finally solved now without changing my database structure.在阅读了@Frank van Puffelen 先生在上面链接的关于这个主题的各种答案并花了一些时间之后,这个问题终于在不改变我的数据库结构的情况下解决了。 Below is screenshot of the result which I wanted:下面是我想要的结果截图:
Here is my modified and working code:这是我修改后的工作代码:
private void retrieveData() {
final String shift = kvName.getText().toString();
final String employeeCode = empCode.getText().toString();
final DatabaseReference aparRef = dbRef.child("Apar").child(shift);
aparRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list = new ArrayList<>();
if (dataSnapshot.exists()) {
for (DataSnapshot dataS1 : dataSnapshot.getChildren()) {
if (dataS1.hasChild(employeeCode)) {
AparData aparData = dataS1.child(employeeCode).getValue(AparData.class);
list.add(aparData);
rvAPAR.setHasFixedSize(true);
rvAPAR.setLayoutManager(new LinearLayoutManager(Apar.this));
rvAPAR.setItemAnimator(new DefaultItemAnimator());
aparAdapter = new AparAdapter(Apar.this, list);
rvAPAR.setAdapter(aparAdapter);
} else {
Toast.makeText(Apar.this, "No Data Found!!", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(Apar.this, "Not Data Found!!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.