How to add data from firestore to my spinner

你。 提交于 2020-07-20 04:23:44

问题


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"

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);

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


回答1:


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?



来源:https://stackoverflow.com/questions/61550428/how-to-add-data-from-firestore-to-my-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!