如何从文档Firestore内部获取字符串列表

时间:2018-11-17 19:57:03

标签: java android firebase google-cloud-firestore

我已经在文档中存储了一个字符串列表。我现在给它起了一个name(“ name”),但我想在客户端获得它,但不知道该怎么做。我可以看到,如果我的字段是数字类型,我可以使用getLong("Fieldname"),但是我无法弄清楚如何获取字符串类型列表。 到目前为止,我的代码

FirebaseFirestore getlistofbrands=FirebaseFirestore.getInstance();
getlistofbrands.collection(FireStoreConstants.COL_BRANDNAMESONLY).document("Brands").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        List<String> listofbrandsfromfirestore=queryDocumentSnapshots.getDocuments().toArray(String.class);

也尝试过

List<String> listofbrandsfromfirestore=queryDocumentSnapshots.toObjects(String.class);

我如何上传数据

   Map < String, Object > docData = new HashMap < > ();
   List < String > deviceNameList = new ArrayList < String > ();
   deviceNameList.add("brand1");
   deviceNameList.add("brand2");
   deviceNameList.add("brand3");
   deviceNameList.add("brand4");
   docData.put("name", deviceNameList);
  firebaseFirestore.collection("FireStoreConstants.COL_BRANDNAMESONLY")
  .document("Brands").set(docData).addOnSuccessListener(new 
  OnSuccessListener < Void > () {
  @Override
  public void onSuccess(Void aVoid) {
  Log.d(TAG, "Repository:onSuccess:");
  }
  }).addOnFailureListener(new OnFailureListener() {
  @Override
  public void onFailure(@NonNull Exception e) {
  Log.d(TAG, "Repository:error" + e.toString());
  }
   });

3 个答案:

答案 0 :(得分:0)

由于您将品牌名称存储在单个文档中,所以我怀疑QuerySnapshot是获取该名称的正确类。据我所知,您将要使用getData()来获取Firestore用于Map<String, Object>的{​​{1}}:

List

您可能还想尝试:

getlistofbrands
  .collection(FireStoreConstants.COL_BRANDNAMESONLY)
  .document("Brands")
  .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());

                Map<String, String> brandsMap = (Map<String, String>)documen.getData().getData("AllBrands");
                List<String> brands = new LinkedList<String>();

                for (String key: brandsMap.keySet()) {
                    brands.add(brandsMap.get(key));
                }

                // TODO: use the brands list
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
  });

答案 1 :(得分:0)

我使用了@Frank van提供的代码,并使用此函数将我的Object转换为字符串列表

 @SuppressWarnings("unchecked")
public static <T extends List<?>> T cast(Object obj) {
    return (T) obj;
}

 Object brandsobj = queryDocumentSnapshots.getData().get("AllBrands");
 List<String> allbrands = cast(brandsobj);

答案 2 :(得分:0)

您可以查看我之前发布的此问题帖子。您可以将我使用的ArrayAdapter更改为List

AutoCompleteTextView data from Firestore

希望它对您有帮助。