我正在创建一个显示主题名称的spinner
。主题名称存储在我的Firestore数据库中,如下所示:
subjects (collection)
|
|--- SUB01 (document)
| |
| |--- name : "Android"
|
|--- SUB02
| |
| |--- name : "Java"
我可以将结果提取到RecyclerView
上,但无法帮助自己做为旋转器。
答案 0 :(得分:1)
要解决此问题,请使用以下代码行:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference subjectsRef = rootRef.collection("subjects");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> subjects = new ArrayList<>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, subjects);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
subjectsRefRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String subject = document.getString("name");
subjects.add(subject);
}
adapter.notifyDataSetChanged();
}
}
});
结果将是一个包含2个项目的微调器:
Android
Java