3

I have an object data structure like below:

enter image description here

These is the code I have retreived the document data:

DocumentReference docRef = db.collection("deyaPayUsers").document(mAuth.getUid()).collection("Split").document(mAuth.getUid()).collection("SentInvitations").document(documentId);
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        Log.d(TAG, "DocumentSnapshot data: " + document.getData());

                        Object value = document.getData();// Here I added the data to the object type value 
                        System.out.println("values"+ value);


                        } else {
                        Log.d(TAG, "No such document");
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });

I have retreived these data from Firestore database. Now I need amount, phone Number and Status in all the Invite(1,2) and add into the list view . I am not able to get these fields first . After that I need to add them to list view . And also whenever user update field Status then listview should also update.

vijju
  • 462
  • 9
  • 30

2 Answers2

2

Assuming that your docRef DocumentReference is correct, to get the values of Amount, PhoneNumber and Status properties, please change the following lines of code:

Object value = document.getData();// Here I added the data to the object type value 
System.out.println("values"+ value);

to

String amount = document.getString("Amount");
String phoneNumber = document.getString("PhoneNumber");
String status = document.getString("Status");
System.out.println(amount + " / " + phoneNumber + " / " + status);

Let's assume you want to get the values of the properties for your Invite1 documents, the output will be:

10 / 9876543210 / Pending

Edit: According to your comment, I understand that you want to get the values of those properties from all documents but in your code you are using the following reference, that points to a single document and not to the entire collection.

DocumentReference docRef = db
    .collection("deyaPayUsers")
    .document(mAuth.getUid())
    .collection("Split")
    .document(mAuth.getUid())
    .collection("SentInvitations")
    .document(documentId); //Reference to a document

See, the last method that is called is .document(documentId)? To get all documents, you need to use a CollectionReference. So please use the following code:

DocumentReference docRef = db
    .collection("deyaPayUsers")
    .document(mAuth.getUid())
    .collection("Split")
    .document(mAuth.getUid())
    .collection("SentInvitations").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Map<String, Object> map = document.getData();
                String amount = map.get("Amount").toString();
                String phoneNumber = map.get("PhoneNumber").toString();
                String status = map.get("Status").toString();
                System.out.println(amount + " / " + phoneNumber + " / " + status);
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

The output will be:

10 / 9876543210 / Pending
20 / 1234566789 / Pending

Edit2:

docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Object> map = document.getData();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    Map<String, Object> m = (Map<String, Object>) entry.getValue();
                    StringBuilder s = new StringBuilder();
                    for (Map.Entry<String, Object> e : m.entrySet()) {
                        s.append(e.getValue() + " ");
                    }
                    Log.d(TAG, s.toString());
                }
            }
        }
    }
});

The output will be:

10 9876543210 Pending
20 1234566789 Pending
//And so on
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I need to get from all the Invites in a document. I need a loop and to store it in variable and add it to the listview – vijju May 04 '18 at 09:43
  • I have Known the documentId no need to use the QuerySnapshot – vijju May 04 '18 at 10:41
  • If you are using the reference in your code, you can get **only** one document, the document with that particular id that is used in your reference `.document(documentId)`. There is no way in which you can loop through other documents using that reference. The **only** way in which you can get the data within all documents is as in my updated answer. So using `QueryDocumentSnapshot` is mandatory, right? – Alex Mamo May 04 '18 at 10:45
  • I dont want to loop other documents .In one document I have many invites I want to loop these invites and get the values – vijju May 04 '18 at 10:55
  • You are not explaining the problem very clearly. To have a quick solve, please add your read database structure and indicate the exact value that you want to get. A screenshot would be enough. – Alex Mamo May 04 '18 at 12:18
  • I have added the screenshot. In the above screenshot there are many Invitees and it contain fields . Now I need to get all the fields from all invitees and add those values to list view. but I am not aware of How to get those values – vijju May 07 '18 at 04:05
  • Now it is more clear. Please see my updated answer. Does it work now? – Alex Mamo May 07 '18 at 07:01
  • K I WILL check it – vijju May 07 '18 at 07:16
  • How can we take each status into an variable – vijju May 08 '18 at 09:34
  • In that case, you should consider create a custom adapter. If you are interested, **[this](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)** is how you can retrieve data from a Cloud Firestore database and display it in a `RecyclerView` using `FirestoreRecyclerAdapter`. – Alex Mamo May 08 '18 at 10:23
  • How csn we get each field into an variable – vijju May 10 '18 at 05:35
  • just like this "String a = document.getString("PhoneNumber")" . how can I get – vijju May 10 '18 at 05:42
  • In the same way as explained in that post. Don't forget also to add more views to hold the values. – Alex Mamo May 10 '18 at 09:21
  • This question was for Android. You need to post another question for ios. – Alex Mamo May 22 '18 at 10:52
  • its ok kkkkkkkk – vijju May 22 '18 at 10:59
1

You can achieve above like below and as per your comments you have your document id then

Invite invite = document.toObject(Invite.class).withId(document.getId());

public class Invite {

    private int amount;
    private String phoneNumber;
    private String status;


    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
Riddhi Shah
  • 477
  • 7
  • 26