![](/img/trans.png)
[英]android firebase database how to retrieve all data from child node
[英]How to retrieve all the data from a node in Android Firebase realtime database?
這是我的 firebase 數據:
我想從 child("Registo Incial e Final") 中檢索所有數據並顯示在表格或列表視圖中。
我來自 android 工作室的代碼:
reff2 = database.getInstance().getReference().child("Registo Inicial e Final");
reff2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange( DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Log.e("MATRICULAAAAAAAAAAAAA", String.valueOf(ds.child("matricula").getValue()));
try {
if (ds.child("matricula").getValue().equals(editText_Matric.getText().toString())) {
String message = ds.child("nome_CONDUTOR").getValue().toString();
String message2 = ds.child("matricula").getValue().toString();
String message3 = ds.child("cod_PROJETO").getValue().toString();
String message4 = ds.child("km_INICIAL").getValue().toString();
String message5 = ds.child("km_FINAL").getValue().toString();
String message6 = ds.child("data_INICIO").getValue().toString();
String message7 = ds.child("data_FIM").getValue().toString();
String message8 = ds.child("hora_INICIO").getValue().toString();
String message9 = ds.child("hora_FIM").getValue().toString();
String message10 = ds.child("observacoes").getValue().toString();
Nome.setText(message);
Matricula.setText(message2);
Cod.setText(message3);
Kilometros_inicial.setText(message4);
Kilometros_final.setText(message5);
Data_inicial.setText(message6);
Data_final.setText(message7);
Hora_inicial.setText(message8);
Hora_final.setText(message9);
Observacoes.setText(message10);
Output:
它僅顯示來自一條記錄的數據(僅來自一個 firebase 按鍵),但我想要來自所有按鍵的所有數據..
您沒有讀取所有數據,因為您指定了一個if-statement
來僅獲取在EditText
中鍵入的相應matricula
字符串的那些:
刪除此if-statement
if (ds.child("matricula").getValue().equals(editText_Matric.getText().toString())) {
......
.....
你會得到所有的值然后可能將它們存儲在一個list
發生的事情是您正在獲取所有孩子的整個快照,然后您的語句if (ds.child("matricula").getValue().equals(editText_Matric.getText().toString()))
被過濾掉所有其他孩子,除了matricula
等於editText_Matric
中的 txt 的地方。 在您的屏幕截圖中,它顯示您的 editText_Matric 文本值為“90-29-VE”,因此您只得到了 Miguel Carvalho 的一個結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.