Java 如何在不使用事件侦听器的情况下从Firestore文档获取单个字段?
这段代码正在工作,但不是我想要的方式,我需要一种方法将单个值(“密钥”)提取到字符串“users”中,而不使用事件侦听器,因为我需要一种方法将此字符串变量发送到另一个函数,以便对其进行加密。我希望注释行中的代码能够正常工作,或者类似的东西Java 如何在不使用事件侦听器的情况下从Firestore文档获取单个字段?,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,这段代码正在工作,但不是我想要的方式,我需要一种方法将单个值(“密钥”)提取到字符串“users”中,而不使用事件侦听器,因为我需要一种方法将此字符串变量发送到另一个函数,以便对其进行加密。我希望注释行中的代码能够正常工作,或者类似的东西 public void getData() { String currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid(); DocumentRefer
public void getData() {
String currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference user = fStore.collection("Users").document(currentUser);
//I WANT THIS TO WORK/////////////////////////////////////////////////////////////////////
String users = fStore.collection("Users").document(currentUser).get("secretKey").toString();
Log.d("LOGGER", users.toString());
//////////////////////////////////////////////////////////////////////////////////////////
user.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
if (value.exists()){
String secretKey = value.getString("secretKey");
String groupID = value.getString("groupID");
String data = secretKey +":::"+ groupID;
StringBuilder textToSend = new StringBuilder();
textToSend.append(data);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(textToSend.toString(), BarcodeFormat.QR_CODE, 600, 600);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
} catch (WriterException e) {
e.printStackTrace();
}
}
}
});
}
public void getData(){
字符串currentUser=FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference user=fStore.collection(“用户”).document(currentUser);
//我希望这能奏效/////////////////////////////////////////////////////////////////////
字符串users=fStore.collection(“users”).document(currentUser.get(“secretKey”).toString();
Log.d(“LOGGER”,users.toString());
//////////////////////////////////////////////////////////////////////////////////////////
user.addSnapshotListener(这个,新的EventListener(){
@凌驾
public void onEvent(@Nullable DocumentSnapshot值,@Nullable FirebaseFirestoreException错误){
if(value.exists()){
String secretKey=value.getString(“secretKey”);
String groupID=value.getString(“groupID”);
字符串数据=secretKey+“:”+groupID;
StringBuilder textToSend=新建StringBuilder();
textToSend.append(数据);
MultiFormatWriter MultiFormatWriter=新的MultiFormatWriter();
试一试{
BitMatrix BitMatrix=multiFormatWriter.encode(textToSend.toString(),BarcodeFormat.QR_代码,600600);
BarcodeCoder BarcodeCoder=新的BarcodeCoder();
位图位图=条形码编码器。创建位图(位图矩阵);
设置图像位图(位图);
设置可见性(View.VISIBLE);
}捕获(写入异常e){
e、 printStackTrace();
}
}
}
});
}
请帮忙!
看待这个问题的另一种方式是:
我需要一种方法将字符串“data”发送到另一个函数。您必须使用
get()
返回的任务异步接收文档的内容,如图所示。您不能只对其调用toString()
。下面是该链接中显示的内容:
DocumentReference docRef = db.collection("cities").document("SF");
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());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
DocumentReference docRef=db.collection(“cities”).document(“SF”);
docRef.get().addOnCompletListener(新的OnCompletListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
DocumentSnapshot document=task.getResult();
if(document.exists()){
Log.d(标记“DocumentSnapshot data:+document.getData());
}否则{
日志d(标签“无此类文件”);
}
}否则{
Log.d(标记“get failed with”,task.getException());
}
}
});
没有比这更短的了
您可以使用DocumentSnapshot对象从文档中获取所需的单个字段。没有只获取单个字段的API-您必须阅读整个文档,然后从中找到所需的字段。如果我使用该API,我无法将“task”的值发送到另一个函数,或者可以吗?您可以在该回调中执行任何您想要的操作。如果需要,您可以从那里调用另一个函数。我的意思是,我可以提取相应的字段,并将其以字符串值的形式发送给另一个函数吗?当然,您可以调用其他函数并将文档的内容作为参数传递。如何?这是不可能的,因为onComplete是空的,它不能返回值字符串