如何使用Java将集合文档的字段值作为Firestore中的值分配给另一个集合文档字段? [英] How to assign field value of a collection document to another collection document field as a value in Firestore using java?

查看:38
本文介绍了如何使用Java将集合文档的字段值作为Firestore中的值分配给另一个集合文档字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个收藏集;预订信息和停车区.从ParkingArea文档中,我想将"areanumber"字段的最新值保存到具有字段名称"Area"的BookingInformation集合中.

I have 2 collections; BookingInformation and ParkingArea. From the ParkingArea document, I want to save the latest value of the "areanumber" field to the BookingInformation collection with the field name "Area".

我尝试了以下方法.在logcat中,我可以查看所需的数据,但不知道如何分配它们.我没有找到任何资源.请帮忙.

I have tried the following. In logcat, I can view the required data but no clue how to assign it. I didn't find any resources. Please help.

final List<String> parkingDocList = new ArrayList<>();

dataStore.collection("ParkingArea").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                parkingDocList.add(documentSnapshot.getId());
            }
        } 
    }
});

final Map<String, Object> reserveInfo = new HashMap<>();

reserveInfo.put("date", date.getText().toString());
reserveInfo.put("startTime", startTime.getText().toString());
reserveInfo.put("endTime", endTime.getText().toString());
reserveInfo.put("userId", currentUserId);
reserveInfo.put("area", allocatedArea[0]);

dataStore.collection("BookingInformation").document(id).set(reserveInfo)
    .addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            Toast toast4 =
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Area reserved successfully", Toast.LENGTH_SHORT);
            toast4.show();
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast toast5 = Toast.makeText(getActivity().getApplicationContext(), "Reservation failed", Toast.LENGTH_SHORT);
            toast5.show();
        }
    });

推荐答案

数据是从Firestore(和大多数云API)异步加载的.这意味着,需要数据库中数据的任何代码都必须在内部加载数据时调用的 onComplete 处理程序内.

Data is loaded from Firestore (and most cloud APIs) asynchronously. This means that any code that needs the data from the database, must be inside the onComplete handler that is called when the data is loaded.

所以在您的情况下:

final List<String> parkingDocList = new ArrayList<>();

dataStore.collection("ParkingArea").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                parkingDocList.add(documentSnapshot.getId());
            }

            final Map<String, Object> reserveInfo = new HashMap<>();

            reserveInfo.put("date", date.getText().toString());
            reserveInfo.put("startTime", startTime.getText().toString());
            reserveInfo.put("endTime", endTime.getText().toString());
            reserveInfo.put("userId", currentUserId);
            reserveInfo.put("area", allocatedArea[0]);

            dataStore.collection("BookingInformation").document(id).set(reserveInfo)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast toast4 =
                                Toast.makeText(getActivity().getApplicationContext(),
                                        "Area reserved successfully", Toast.LENGTH_SHORT);
                        toast4.show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast toast5 = Toast.makeText(getActivity().getApplicationContext(), "Reservation failed", Toast.LENGTH_SHORT);
                        toast5.show();
                    }
                });
        } 
    }
});

这篇关于如何使用Java将集合文档的字段值作为Firestore中的值分配给另一个集合文档字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆