【问题标题】:Query the array elements in the firestore and place it in the Android Studio array (CollectionReference) (whereArrayContains)查询firestore中的数组元素并将其放入Android Studio数组(CollectionReference)(whereArrayContains)
【发布时间】:2019-10-22 22:34:22
【问题描述】:

我想获取数组的元素并将其放入我在应用程序中使用的 Java 文件中的一个数组中我想在 Java 文件中创建一个数组,从中导入相同的元素。

package com.AZERTYQSD.phm;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PAGE40 extends AppCompatActivity {
    public static Bundle  sss = new  Bundle () ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page40);


        FirebaseFirestore db = FirebaseFirestore.getInstance();

        CollectionReference cities = db.collection("cities");

        Map<String, Object> data1 = new HashMap<>();
        data1.put("name", "San Francisco");
        data1.put("state", "CA");
        data1.put("country", "USA");
        data1.put("capital", false);
        data1.put("population", 860000);
        data1.put("regions", Arrays.asList("west_coast", "norcal"));
        cities.document("SF").set(data1);

        Map<String, Object> data2 = new HashMap<>();
        data2.put("name", "Los Angeles");
        data2.put("state", "CA");
        data2.put("country", "USA");
        data2.put("capital", false);
        data2.put("population", 3900000);
        data2.put("regions", Arrays.asList("west_coast", "socal"));
        cities.document("LA").set(data2);

        Map<String, Object> data3 = new HashMap<>();
        data3.put("name", "Washington D.C.");
        data3.put("state", null);
        data3.put("country", "USA");
        data3.put("capital", true);
        data3.put("population", 680000);
        data3.put("regions", Arrays.asList("east_coast"));
        cities.document("DC").set(data3);

        Map<String, Object> data4 = new HashMap<>();
        data4.put("name", "Tokyo");
        data4.put("state", null);
        data4.put("country", "Japan");
        data4.put("capital", true);
        data4.put("population", 9000000);
        data4.put("regions", Arrays.asList("kanto", "honshu"));
        cities.document("TOK").set(data4);

        Map<String, Object> data5 = new HashMap<>();
        data5.put("name", "Beijing");
        data5.put("state", null);
        data5.put("country", "China");
        data5.put("capital", true);
        data5.put("population", 21500000);
        data5.put("regions", Arrays.asList("jingjinji", "hebei"));
        cities.document("BJ").set(data5);


        CollectionReference citiesRef = db.collection("cities");

        citiesRef.whereArrayContains("west_coast", "west_coast").get();
        sss.putIntArray("npdf" , citiesRef.document("west_coast"));



    }
}

【问题讨论】:

  • 您在寻求get 查询方面的帮助吗?您需要添加一个监听器:docRef.get().addOnSuccessListener(...)。文档在这里:firebase.google.com/docs/firestore/query-data/get-data
  • 您是否尝试过按照 AjahnCharles 的建议附加监听器?
  • 我有知识我不知道怎么做 是否可以给我写一个代码来试试?我想将云数组元素移动到我的应用程序中的数组

标签: java android firebase google-cloud-firestore


【解决方案1】:

最简单和最优雅的方法是使用数组创建一个对象,然后将文档转换为该对象。

例如:

获取文档:

db.collection("notes").get().addOnCompleteListener(task -> {
                       if(task.isSuccessful()){
                           DocumentSnapshot documentSnapshot = task.getResult();
                           assert documentSnapshot != null;
                            Note note = documentSnapshot.toObject(Note.class);
                            if(note!=null) {
                                note.getHistory();
                                // this is the array in java. 
                            }
                       }

                    });

一个对象:

package com.example.firebaseui_firestoreexample;

import com.google.firebase.Timestamp;

import java.util.ArrayList;
import java.util.Date;

@SuppressWarnings("WeakerAccess")
public class Note {
    private String title;
    private String description;
    private ArrayList<String> history;
    private ArrayList<String> titleHistory;
    private ArrayList<String> shared;
    private boolean keepOffline;
    private boolean loadToCache;
    private boolean trash;
    private Timestamp created;
    private String creator;


    @SuppressWarnings("unused")
    public Note(){
        // empty constructor needed for firebase
    }
    public Note(String title, String description, Timestamp created, String creator) {
        this.title = title;
        this.description = description;
        this.created = created;
        this.creator = creator;
        history = new ArrayList<>();
        titleHistory = new ArrayList<>();
        shared = new ArrayList<>();
        keepOffline = false;
        loadToCache = false;
        trash = false;
    }

    public Note newNoteVersion(){
        return new Note(title,description, new Timestamp(new Date()), creator);
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }


    public ArrayList<String> getHistory() {
        return history;
    }
    public ArrayList<String> getTitleHistory() {
        return titleHistory;
    }

    public Timestamp getCreated() {
        return created;
    }

    public boolean isKeepOffline() {
        return keepOffline;
    }

    public boolean isLoadToCache() {
        return loadToCache;
    }

    public String getCreator() {
        return creator;
    }

    public ArrayList<String> getShared() {
        return shared;
    }

    public boolean isTrash() {
        return trash;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2019-08-23
    • 2023-03-14
    • 2020-03-26
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多