[英]How can I populate the 'Spinner' with a list of associated node names from the Firebase Realtime Database?
Let's say I have the following firebase realtime database structure:假设我有以下 firebase 实时数据库结构:
{
"Object1" : {
"A" : "exampleData",
"B" : "exampleData",
"C" : "exampleData",
"D" : "exampleData"
}
}
I'm trying to get a list of associated node names from "Object1".我正在尝试从“Object1”获取关联节点名称的列表。 I'm trying Inflate Spinner (which is part of my layout.xml) with this list of associated node names.
我正在尝试使用此关联节点名称列表的 Inflate Spinner(它是我的 layout.xml 的一部分)。 The spinner will therefore contain items to choose from: A, B, C, D
因此,微调器将包含可供选择的项目:A、B、C、D
I'm trying like this: (Just a snippet)我正在尝试这样:(只是一个片段)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState {
super.onCreate(savedInstanceState);
setContentView(R.layout.MyActivity);
Spinner spinner = findViewById(R.id.MySpinner);
database = FirebaseDatabase.getInstance();
dbRef = database.getReference("Object1");
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> arrList = new ArrayList<String>();
for (DataSnapshot snap : dataSnapshot.getChildren()) {
arrList.add(snap.getKey());
}
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(MyActivity.this,R.layout.MyActivity, arrList);
spinner.setAdapter(arrAdapter);
}
});
}
After running this activity, the application "crashes" and an error occurs.运行此活动后,应用程序“崩溃”并出现错误。 If necessary, I am also able to supply an extract from LogCat.
如有必要,我还可以提供 LogCat 的摘录。
Expectation: Retrieve a list of nodes associated with Object1, populate the spinner with this list.
期望:检索与 Object1 关联的节点列表,使用此列表填充微调器。
Here is a report from Logcat:这是来自 Logcat 的报告:
(just errors) (只是错误)
E/RunLoop: Uncaught exception in Firebase Database runloop (3.0.0). Please report to firebase-database-client@google.com
java.lang.NoClassDefFoundError: com.google.firebase.database.android.AndroidAuthTokenProvider$3
at com.google.firebase.database.android.AndroidAuthTokenProvider.produceIdTokenListener(com.google.firebase:firebase-database@@16.0.4:85)
at com.google.firebase.database.android.AndroidAuthTokenProvider.addTokenChangeListener(com.google.firebase:firebase-database@@16.0.4:80)
at com.google.firebase.database.core.Repo.deferredInitialization(com.google.firebase:firebase-database@@16.0.4:109)
at com.google.firebase.database.core.Repo.access$000(com.google.firebase:firebase-database@@16.0.4:55)
at com.google.firebase.database.core.Repo$1.run(com.google.firebase:firebase-database@@16.0.4:94)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
You have supplied the layout file of your activity in您已经提供了活动的布局文件
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(MyActivity.this,R.layout.MyActivity, arrList);
The Second argument of the ArrayAdapter<>() requires a resource ID of a TextView as stated in the Error Message ArrayAdapter<>() 的第二个参数需要 TextView 的资源 ID,如错误消息中所述
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
java.lang.IllegalStateException: ArrayAdapter 要求资源 ID 为 TextView
You can use R.layout.support_simple_spinner_dropdown_item instead as below您可以使用 R.layout.support_simple_spinner_dropdown_item 代替如下
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(MyActivity.this,R.layout.support_simple_spinner_dropdown_item,arrList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.