如何从 Firebase 中的第二个生成的密钥中获取数据?
How to get data from second generated key in Firebase?
我想获取黄色标记的数据,然后显示在Android的recyclerView中,但不知道如何获取随机生成的Key红色标记的push()
我看过各种视频、教程和文档,但我完成最多的是显示以绿色标记的数据。
我附上了显示绿色标记内容的代码(我希望能够以黄色显示数据)
public class mostrarActivity extends AppCompatActivity {
RecyclerView recyclerPa;
DatabaseReference database;
Adapter adapter;
ArrayList<Pacientes> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mostrar);
recyclerPa = findViewById(R.id.RecyclerPa);
database = FirebaseDatabase.getInstance().getReference("Usuarios");
recyclerPa.setHasFixedSize(true);
recyclerPa.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
adapter = new Adapter(this,list);
recyclerPa.setAdapter(adapter);
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
Pacientes pacientes = dataSnapshot.getValue(Pacientes.class);
list.add(pacientes);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
由于您要显示的数据嵌套在两层动态键下,因此您需要在 getChildren()
.
上进行两个嵌套循环
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot userSnapshot : snapshot.getChildren()){
for (DataSnapshot patientSnapshot : userSnapshot.getChildren()){ // add an extra loop
if (patientSnapshot.hasChild("nombre")) { // check for object
Pacientes pacientes = patientSnapshot.getValue(Pacientes.class);
if (pacientes != null) { // skip non-patient nodes (see note below)
list.add(pacientes);
}
}
}
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // Never ignore errors
}
});
一般来说,您的数据结构违反了 Firebase not building nested data, so I recommend studying the documentation on structuring data and watching David's excellent video series on Firebase for SQL developers 的建议。
我想获取黄色标记的数据,然后显示在Android的recyclerView中,但不知道如何获取随机生成的Key红色标记的push()
我看过各种视频、教程和文档,但我完成最多的是显示以绿色标记的数据。
我附上了显示绿色标记内容的代码(我希望能够以黄色显示数据)
public class mostrarActivity extends AppCompatActivity {
RecyclerView recyclerPa;
DatabaseReference database;
Adapter adapter;
ArrayList<Pacientes> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mostrar);
recyclerPa = findViewById(R.id.RecyclerPa);
database = FirebaseDatabase.getInstance().getReference("Usuarios");
recyclerPa.setHasFixedSize(true);
recyclerPa.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
adapter = new Adapter(this,list);
recyclerPa.setAdapter(adapter);
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
Pacientes pacientes = dataSnapshot.getValue(Pacientes.class);
list.add(pacientes);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
由于您要显示的数据嵌套在两层动态键下,因此您需要在 getChildren()
.
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot userSnapshot : snapshot.getChildren()){
for (DataSnapshot patientSnapshot : userSnapshot.getChildren()){ // add an extra loop
if (patientSnapshot.hasChild("nombre")) { // check for object
Pacientes pacientes = patientSnapshot.getValue(Pacientes.class);
if (pacientes != null) { // skip non-patient nodes (see note below)
list.add(pacientes);
}
}
}
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // Never ignore errors
}
});
一般来说,您的数据结构违反了 Firebase not building nested data, so I recommend studying the documentation on structuring data and watching David's excellent video series on Firebase for SQL developers 的建议。