如何将文档字段数据从Firestore数据库中的特定集合存储到Kotlin中的字符串变量

时间:2019-12-19 08:16:28

标签: android firebase kotlin google-cloud-firestore

我想从云存储库中获取数据,并将其存储到字符串变量中,但无法这样做,并且需要帮助。

这是下面的代码

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                matchedGrpToDonate = spinner.getSelectedItem().toString();
                matchedBloodGroups();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
private void matchedBloodGroups() {
        if (matchedGrpToDonate.equals("A+")) {
            bloodBankCanDonate.add("AB+");
            bloodBankCanDonate.add("A+");
            System.out.println("hash" + bloodBankCanDonate);
        } else if (matchedGrpToDonate.equals("A+")) {
            bloodBankCanDonate.remove("AB+");
            bloodBankCanDonate.remove("A+");
            System.out.println("hash" + bloodBankCanDonate);
        }

        if (matchedGrpToDonate.equals("A-")) {
            bloodBankCanDonate.add("AB+");
            bloodBankCanDonate.add("A+");
            bloodBankCanDonate.add("AB-");
            bloodBankCanDonate.add("A-");
            System.out.println("hash" + bloodBankCanDonate);
        } else if (matchedGrpToDonate.equals("A-")) {
            bloodBankCanDonate.remove("AB+");
            bloodBankCanDonate.remove("A+");
            bloodBankCanDonate.remove("AB-");
            bloodBankCanDonate.remove("A-");
            System.out.println("hash" + bloodBankCanDonate);
        }

        if (matchedGrpToDonate.equals("B+")) {
            bloodBankCanDonate.add("AB+");
            bloodBankCanDonate.add("B+");
            System.out.println("hash" + bloodBankCanDonate);
        } else if (matchedGrpToDonate.equals("B+")) {
            bloodBankCanDonate.remove("AB+");
            bloodBankCanDonate.remove("B+");
            System.out.println("hash" + bloodBankCanDonate);
        }

        if (matchedGrpToDonate.equals("B-")) {
            bloodBankCanDonate.add("AB+");
            bloodBankCanDonate.add("B+");
            bloodBankCanDonate.add("AB-");
            bloodBankCanDonate.add("B-");
            System.out.println("hash" + bloodBankCanDonate);
        } else if (matchedGrpToDonate.equals("B-")) {
            bloodBankCanDonate.remove("AB+");
            bloodBankCanDonate.remove("B+");
            bloodBankCanDonate.remove("AB-");
            bloodBankCanDonate.remove("B-");
            System.out.println("hash" + bloodBankCanDonate);
        }

        if (matchedGrpToDonate.equals("O+")) {
            bloodBankCanDonate.add("AB+");
            bloodBankCanDonate.add("A+");
            bloodBankCanDonate.add("B+");
            bloodBankCanDonate.add("O+");
            System.out.println("hash" + bloodBankCanDonate);

        } else if (matchedGrpToDonate.equals("O+")) {
            bloodBankCanDonate.remove("AB+");
            bloodBankCanDonate.remove("A+");
            bloodBankCanDonate.remove("B+");
            bloodBankCanDonate.remove("O+");
            System.out.println("hash" + bloodBankCanDonate);
        }

        if (matchedGrpToDonate.equals("O-")) {
            bloodBankCanDonate.add("AB+");
            bloodBankCanDonate.add("A+");
            bloodBankCanDonate.add("B+");
            bloodBankCanDonate.add("O+");
            bloodBankCanDonate.add("AB-");
            bloodBankCanDonate.add("A-");
            bloodBankCanDonate.add("B-");
            bloodBankCanDonate.add("O-");
            System.out.println("hash" + bloodBankCanDonate);
        } else if (matchedGrpToDonate.equals("O-")) {
            bloodBankCanDonate.remove("AB+");
            bloodBankCanDonate.remove("A+");
            bloodBankCanDonate.remove("B+");
            bloodBankCanDonate.remove("O+");
            bloodBankCanDonate.remove("AB-");
            bloodBankCanDonate.remove("A-");
            bloodBankCanDonate.remove("B-");
            bloodBankCanDonate.remove("O-");
            System.out.println("hash" + bloodBankCanDonate);
        }
    }

从数据库中获取此文件,我希望链接存储到变量first_image

val db = FirebaseFirestore.getInstance()
        val docRef = db.collection("SlideShowImages").document("1")
        docRef.get()
            .addOnSuccessListener { document ->
                if (document != null) {
                    Log.d(TAG, "DocumentSnapshot data: ${document.data}")
                } else {
                    Log.d(TAG, "No such document")
                }
            }
            .addOnFailureListener { exception ->
                Log.d(TAG, "get failed with ", exception)
            }

我想将其存储在变量中

{about=FCB, link=https://firebasestorage.googleapis.com/v0/b/missionx-g6305.appspot.com/o/EVENTS%2FSlide%20Show%20Images%2FCurrent%2FFCB.jpg?alt=media&token=950c5200-c553-4639-b33e-2b91a220b19c}

使用此工具时出现错误

1 个答案:

答案 0 :(得分:1)

并放入变量类型var,以便您可以修改值。 Val仅允许设置与该变量相关的值。

private var first_image: String

并在您的变量中获取图片:

first_image = document.getString("link");

读取val and var之间的差异。