Get all fields in a document in a list - Firestore

2020-07-30 04:02发布

问题:

I am trying to get all fields from a document to a ListView. I have tried foreach loop but it is not working.

dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com")
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            // Get all fields to a list
        }
    });

回答1:

To add all the value of all properties within a document to a list, please use the followig lines of code:

dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                List<String> list = new ArrayList<>();

                Map<String, Object> map = document.getData();
                if (map != null) {
                    for (Map.Entry<String, Object> entry : map.entrySet()) {
                        list.add(entry.getValue().toString());
                    }
                }

                //So what you need to do with your list
                for (String s : list) {
                    Log.d("TAG", s);
                }
            }
        }
    }
});


回答2:

To get the fields use the following:

DocumentReference docRef = db.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com");
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());
            Log.d(TAG,"String value: " + document.getString("names"));
        } else {
            Log.d(TAG, "No such document");
        }
    } else {
        Log.d(TAG, "get failed with ", task.getException());
    }
  }
});

public Map<String, Object> getData ()

Returns the fields of the document as a Map or null if the document doesn't exist. Field values will be converted to their native Java representation.

Or you can use getString()



回答3:

Try This

     List<Type> types = documentSnapshots.toObjects(Type.class);   

For your example will be like this

dbRef.collection("Shopkeeper Own Shops").document("Shopkeeper@gmail.com")
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            if(documentSnapshots.isEmpty()){
              Log.d("MyLogger", "Empty Document!");
            }else{
            // Get all fields to a list
             List<MyModel> types = documentSnapshots.toObjects(MyModel.class); 
           }  
        }
    });

public class MyModel{
  // Define fields
  private String id;
  private String name; // ...etc

  // GETTER/SETTER, 
}


回答4:

Try this,

Make sure you have internet permission in manifest, And your project is connected to firebase.

private static final String KEY_NAME = "name";
public void loadName() {

    db.collection("users").document()
            .get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document =  task.getResult();
                        String name = document.getString(KEY_NAME);

                    } else {
                        Toast.makeText(activity_home.this, "Error", Toast.LENGTH_LONG).show();
                    }
                }
            });
}