如何将数据从firestore添加到我的微调器中



应用程序中的登录仅通过otp方法完成。因此,首先我检索手机号码注册的社会。然后我只想用那个社会的所有塔名填充我的旋转器。接收社团名称的消防仓库形式的结构是

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中输入了一些本应显示的示例数据。请告诉我这个哪里出错了

我相信这篇文章可能会有所帮助,因为它展示了如何用Firestore查询的结果填充微调器:如何用Firestore查询的结果来填充微调器?

最新更新