Get data from Cloud Firestore

Viewed 1226

Picture

I need to get data from Firestore Firebase, I need to get the value of field "Company" from "employer" collection -> "TH17..." document. I try some things but it didnt work.

How I can do this?

I try this:

docref = db.collection("employer").document("TH17...");
        docref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        String p= document.getData().toString();
                        Log.d("TAG", "DocumentSnapshot data: " + document.getData());
                    } 
                }
            }
        });
1 Answers

I need to get the value of field "company" from "employer" collection -> "TH17..." document.

To solve this, please change the following lines of code:

String p= document.getData().toString();
Log.d("TAG", "DocumentSnapshot data: " + document.getData());

to

String company = document.getString("Company");
Log.d("TAG", "Company: " + company);

The result in your logcat will be:

Company: Test
Related