gpt4 book ai didi

java - 查询firestore中的数组元素并将其放入Android Studio数组(CollectionReference)(whereArrayContains)

转载 作者:行者123 更新时间:2023-11-30 12:02:31 26 4
gpt4 key购买 nike

enter image description here

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"));



}
}

最佳答案

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

例如:

获取文档:

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

关于java - 查询firestore中的数组元素并将其放入Android Studio数组(CollectionReference)(whereArrayContains),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58513144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com