gpt4 book ai didi

java - 如何将数据从 firestore 添加到我的 spinner

转载 作者:行者123 更新时间:2023-12-01 16:58:32 28 4
gpt4 key购买 nike

应用程序的登录仅通过otp方法完成。所以首先我检索手机号码所在的社会。已登记。然后我只想用该协会的所有塔名称填充我的旋转器。接收社团名称的 firestore 表单的结构是

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

以及具有社会名称的集合的结构

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

我目前使用的主要 Activity 代码是这里的 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 查询的结果填充微调器:How to populate a spinner with the result of a Firestore query?

关于java - 如何将数据从 firestore 添加到我的 spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61550428/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com