简体   繁体   中英

How to get a field which is an object from Firestore?

I have a database like this:

在此处输入图片说明

and I want to retrieve that q8, I already made a Class in my app with all those fields:

public class Question {

    boolean completed;
    String hint;
    String hintImage;
    String hintimagename;
    String id;
    String imagePathWeb;
    String passw;
    String strikes;

    public Question(boolean completed, String hint, String hintImage, String hintimagename, String id, String imagePathWeb, String passw, String strikes) {
... ...
}

and the getters and setters

I have many of these qs like q1,q2,q3...q20 and I want them to be stored in an List.

I'm stuck here:

   db.collection("games").document(huntPlayID).addSnapshotListener(new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot snapshot,
                                @Nullable FirebaseFirestoreException e) {
                if(e!=null) {
                    System.err.println("Listen Failed:" + e);
                    return;
                }

                if(snapshot != null && snapshot.exists()) {
                    changeUI(snapshot);
                } else {
                    System.out.print("curent data: null");
                }
            }
        });


 private void changeUI(final DocumentSnapshot game) {
 Question q1 = new Question(
    // game.get("q1".completed ??
       );
}

game is my document, but how can I access that completed field of q8 ? or any others fields? How can I access those values and insert them into my Question Object.. Thank you for your time!

Thank you in advance!

When get q1 entirely you get back a Map<String,Object> that you then can get the subfields from:

Map<String,Object> q1 = (Map<String,Object>) snapshot.get("q1");
bool q1completed = (bool) q1["completed"]

I think you can use also dot notation to address nested fields directly:

snapshot.get("q1.completed")

The best solution will be to reorganise your firestore document and use array instead of map. Remember, you are charged for every document load, if you load the whole document or 1 field the price is the same. When loading you will need to do this:

private static class Questions {
    Arraylist<Question> questions;
    // add constructor + getter 
}

private ArrayList<Questions> arr; private void changeUI(final DocumentSnapshot game) {
    Questions questions = game.toObject(Questions.class);
    arr = questions.getQuestions();  
}

If you still want to read fields one by one you should do this:

private void changeUI(final DocumentSnapshot game) {
    Question question = game.get("q1").toObject(Question.class); 
}

But you should remember that 2nd option will be much more expensive

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