【问题标题】:How to add data from firestore to my spinner如何将数据从 Firestore 添加到我的微调器
【发布时间】:2020-08-16 09:53:11
【问题描述】:

应用程序中的登录仅通过 otp 方法完成。 所以首先我检索了手机号码所在的社会。已经注册。 然后我只想用那个社会的所有塔名填充我的微调器。 接收社团名称的firestore表单的结构是

     mobile
       |-----(mobile no.)
       |          |----society:"name of society"
       |

以及带有社团名称的馆藏结构

   tower_list
       |----(name of tower)
       |           |-----name:"name of tower"
       |           |-----society:"name of society"

我目前使用的主要活动代码是 这里 fAuth 是 firebase 身份验证的实例 mtower 是我的微调器;

phone = fAuth.getCurrentUser().getPhoneNumber();
DocumentReference docRef = db.collection("mobile").document(phone);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                society = (String) document.get("society").toString();
            } else {
                startActivity(new Intent(getApplicationContext(), otp.class));
                finish();
            }
        } else {
            Toast.makeText(MainActivity.this, "SERVER ERROR! Society not retrieved", Toast.LENGTH_SHORT).show();
        }
    }
});
tower = new ArrayList<>();
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, tower);
Query query = db.collection("tower_list");
progressBar3.setVisibility(View.VISIBLE);
db.collection("tower_list").whereEqualTo("society",society)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                QuerySnapshot doc = task.getResult();
                for (DocumentSnapshot documentSnapshot : doc.getDocuments()){
                    String tower_name = documentSnapshot.get("name").toString();
                    tower.add(tower_name);
                }
            }
        });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        mtower.setAdapter(adapter);

在运行此代码时,微调器不包含任何值,但我在 firestore 中输入了一些应该显示的示例数据。 请告诉我哪里错了

【问题讨论】:

    标签: java android firebase google-cloud-firestore firebase-authentication


    【解决方案1】:

    我相信这篇文章可能会有所帮助,因为它展示了如何使用 Firestore 查询的结果填充微调器:How to populate a spinner with the result of a Firestore query?

    【讨论】:

    • 我只在看到提供的链接后编写了代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多