简体   繁体   中英

Firebase retrieve data from 2 levels in Firebase Realtime Database

I'm trying to retrieve data from Firebase Realtime database into a table called "joueur" but i have the following error:

com.google.firebase.database.DatabaseException: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.florian.fichiertexteandbddtest1, PID: 899 com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.example.florian.fichiertexteandbddtest1.Joueur

My Firebase Realtime Database is this one: firebase realtime database

I'm trying to get the value of joueurs inside the children of tables Joueurs

Here is my code to retrieve the data and add it to the table:

int i;
            for (i=0;i<150;i++) {
                firebaseReference2.child(String.valueOf(i));
                firebaseReference2.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        List<String> keys = new ArrayList<>();
                        for (DataSnapshot keyNode : snapshot.getChildren()) {
                            keys.add(keyNode.getKey());
                            Joueur joueur = keyNode.getValue(Joueur.class);
                            databaseHelper.addJoueur(joueur.getNom_joueur(), joueur.getPrenom_joueur(), joueur.getNum_licence_joueur(), joueur.getId_equipe_joueur());
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        Toast.makeText(EquipeActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

The error is on Joueur joueur = keyNode.getValue(Joueur.class);

Also my Joueur class: `public class Joueur implements Serializable {

private String nom_joueur;
private String prenom_joueur;
private int num_licence_joueur;
private int id_equipe_joueur;
private int id;


public Joueur() {
}

public Joueur(String nom_joueur, String prenom_joueur, int num_licence_joueur, int id_equipe_joueur, int id) {
    this.nom_joueur = nom_joueur;
    this.prenom_joueur = prenom_joueur;
    this.num_licence_joueur = num_licence_joueur;
    this.id_equipe_joueur = id_equipe_joueur;
    this.id = id;
}

public String getNom_joueur() {
    return nom_joueur;
}

public void setNom_joueur(String nom_joueur) {
    this.nom_joueur = nom_joueur;
}

public String getPrenom_joueur() {
    return prenom_joueur;
}

public void setPrenom_joueur(String prenom_joueur) {
    this.prenom_joueur = prenom_joueur;
}

public int getNum_licence_joueur() {
    return num_licence_joueur;
}

public void setNum_licence_joueur(int num_licence_joueur) {
    this.num_licence_joueur = num_licence_joueur;
}

public int getId_equipe_joueur() {
    return id_equipe_joueur;
}

public void setId_equipe_joueur(int id_equipe_joueur) {
    this.id_equipe_joueur = id_equipe_joueur;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

}`

Thank you!

To be able to get the data according to your database schema, you need to loop through the "DataSnapshot" object using ".getChildren()" twice, as explained in the following lines of code:

firebaseReference2.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            List<String> keys = new ArrayList<>();
            for (DataSnapshot firstLevelSnapshot : task.getResult().getChildren()) {
                for (DataSnapshot secondLevelSnapshot : firstLevelSnapshot.getChildren()) {
                    String key = secondLevelSnapshot.getKey();
                    keys.add(key);
                    Joueur joueur = secondLevelSnapshot.getValue(Joueur.class);
                    databaseHelper.addJoueur(
                        joueur.getNom_joueur(),
                        joueur.getPrenom_joueur(),
                        joueur.getNum_licence_joueur(),
                        joueur.getId_equipe_joueur()
                    );
                    Log.d(TAG, secondLevelSnapshot.getNom_joueur());
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result of the above code will be the passing of each "Joueur" object to the "addJoueur()" method and in the logcat you'll see the following output:

ccu
...

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