Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查询firestore中的数组元素,并将其放入Android Studio数组(CollectionReference)(WhererrayContains)_Java_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Java 查询firestore中的数组元素,并将其放入Android Studio数组(CollectionReference)(WhererrayContains)

Java 查询firestore中的数组元素,并将其放入Android Studio数组(CollectionReference)(WhererrayContains),java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我想获取数组的元素,并将其放入我在应用程序中使用的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.a

我想获取数组的元素,并将其放入我在应用程序中使用的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"));



    }
}

package com.AZERTYQSD.phm;
导入androidx.annotation.NonNull;
导入androidx.appcompat.app.appcompat活动;
导入android.os.Bundle;
导入android.util.Log;
导入com.google.android.gms.tasks.OnCompleteListener;
导入com.google.android.gms.tasks.Task;
导入com.google.firebase.firestore.CollectionReference;
导入com.google.firebase.firestore.DocumentSnapshot;
导入com.google.firebase.firestore.FirebaseFirestore;
导入com.google.firebase.firestore.Query;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共类第40页扩展AppCompative活动{
公共静态Bundle sss=新Bundle();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.page40);
FirebaseFirestore db=FirebaseFirestore.getInstance();
CollectionReference cities=db.collection(“城市”);
Map data1=新的HashMap();
数据1.put(“名称”、“旧金山”);
数据1.出售(“州”、“CA”);
数据1.put(“国家”、“美国”);
数据1.认沽期权(“资本”,假);
数据1.put(“人口”,860000);
data1.put(“regions”,Arrays.asList(“west_coast”,“norcal”);
城市文件集(数据1);
Map data2=新的HashMap();
数据2.put(“名称”、“洛杉矶”);
数据2.出售(“州”、“CA”);
数据2.put(“国家”、“美国”);
数据2.认沽期权(“资本”,虚假);
数据2.put(“人口”,3900000);
数据2.put(“区域”,数组.asList(“西海岸”,“socal”);
城市。文件(“LA”)。集(数据2);
Map data3=新的HashMap();
数据3.put(“名称”,“华盛顿特区”);
数据3.put(“状态”,空);
数据3.put(“国家”、“美国”);
数据3.出售(“资本”,真实);
数据3.put(“人口”,680000);
data3.put(“regions”,Arrays.asList(“east_coast”);
城市。文件(“DC”)。集合(数据3);
Map data4=新的HashMap();
数据4.put(“名称”、“东京”);
数据4.put(“状态”,空);
数据4.出售(“国家”、“日本”);
数据4.出售(“资本”,真实);
数据4.put(“人口”,9000000);
数据4.put(“regions”,Arrays.asList(“kanto”,“honshu”);
城市文件(“TOK”).set(数据4);
Map data5=新的HashMap();
数据5.put(“名称”、“北京”);
数据5.put(“状态”,空);
数据5.出售(“国家”、“中国”);
数据5.认沽(“资本”,真实);
数据5.put(“人口”,21500000);
data5.put(“regions”,Arrays.asList(“京津冀”,“河北”);
城市文件集(数据5);
CollectionReference citiesRef=db.collection(“城市”);
citiesRef.WhererrayContains(“西海岸”、“西海岸”).get();
putIntArray sss(“npdf”,城市参考文件(“西海岸”);
}
}

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

例如:

获取文档:

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;
    }
}

package com.example.firebaseui\u firestoreexample;
导入com.google.firebase.Timestamp;
导入java.util.ArrayList;
导入java.util.Date;
@抑制警告(“弱化访问”)
公开课堂讲稿{
私有字符串标题;
私有字符串描述;
私家侦探史;
私人ArrayList titleHistory;
私有ArrayList共享;
私有布尔keepOffline;
私有布尔加载缓存;
私人布尔垃圾;
创建私有时间戳;
私有字符串创建者;
@抑制警告(“未使用”)
公共说明(){
//firebase需要空构造函数
}
公共注释(字符串标题、字符串描述、创建的时间戳、字符串创建者){
this.title=标题;
this.description=描述;
this.created=created;
this.creator=创建者;
历史=新的ArrayList();
titleHistory=新ArrayList();
共享=新的ArrayList();
keepOffline=false;
loadToCache=false;
垃圾=假;
}
公共注释newNoteVersion(){
返回新注释(标题、说明、新时间戳(新日期())、创建者);
}
公共字符串getTitle(){
返回标题;
}
公共字符串getDescription(){
返回说明;
}
公共阵列列表getHistory(){
回归历史;
}
公共ArrayList getTitleHistory(){
返回标题历史;
}
公共时间戳getCreated(){
创建回报;
}
公共布尔值isKeepOffline(){
返回keepOffline;
}
公共布尔值isLoadToCache(){
返回loadToCache;
}
公共字符串getCreator(){
回归创造者;
}
公共ArrayList getShared(){
收益共享;
}
公共布尔值(){
回收垃圾;
}
}

您是否在请求有关
get
查询的帮助?您需要添加一个侦听器:
docRef.get().addOnSuccessListener(…)
。文档在这里:你有没有像AjahnCharles建议的那样尝试附加一个监听器?我知道我不知道怎么做。有没有可能给我写一个代码来尝试呢?我想将云数组元素移动到我的应用程序中的数组中