如何将数据从Firestore添加到我的微调器 [英] How to add data from firestore to my spinner

查看:56
本文介绍了如何将数据从Firestore添加到我的微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅通过otp方法登录应用程序. 因此,首先我要检索移动电话所在的社会.已经注册. 那么我只想用该社会的所有塔式名称填充我的微调器. 收到社会名称的消防站形式的结构是

Login in the app is solely done by otp method. so first i retrieve the society in the which the mobile no. is registered. then i want to populate my spinner with all tower names of that society only. the structure of firestore form where society name is received is

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

以及具有社会名称的馆藏结构

and the structure of the collection having the names of society

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

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

the main activity code i am using at present is here fAuth is instance of firebase authentication and mtower is my spinner;

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中输入了一些应显示的示例数据. 请告诉我我要去哪里错了

on running this code the spinner contains no value but i had entered some sample data in firestore which should have been displayed. kindly tell where am i going wrong in this

推荐答案

我认为这篇文章可能会对您有所帮助,因为它展示了如何使用Firestore查询的结果填充微调框:

I believe this post may be helpful as it shows how to populate a spinner with the result of a Firestore query: How to populate a spinner with the result of a Firestore query?

这篇关于如何将数据从Firestore添加到我的微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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