简体   繁体   中英

How to retrieve all the data from a node in Android Firebase realtime database?

This is my firebase data:

在此处输入图像描述

I wanted to retrieve all the data from child("Registo Inicial e Final") and display in a table or in a list view.

My code from android studio:

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:

It´s only showing the data from only one record (only from one firebase push key), but I want all the data from all push keys..

在此处输入图像描述

You are not reading all the data because you specified an if-statement to get only those for the corresponding matricula string that is typed in the EditText :

Remove this if-statement

if (ds.child("matricula").getValue().equals(editText_Matric.getText().toString())) {

......
.....

And you will get all the values then maybe store them in a list

What is happening is you're getting the whole snapshot with all the children, then your statement if (ds.child("matricula").getValue().equals(editText_Matric.getText().toString())) is filtering out all the other children, except where matricula is equal to the txt in editText_Matric . In your screenshot, it shows that your editText_Matric text value was "90-29-VE", so you only got that one result for Miguel Carvalho.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM