[英]How can I populate the 'Spinner' with a list of associated node names from the Firebase Realtime Database?
假設我有以下 firebase 實時數據庫結構:
{
"Object1" : {
"A" : "exampleData",
"B" : "exampleData",
"C" : "exampleData",
"D" : "exampleData"
}
}
我正在嘗試從“Object1”獲取關聯節點名稱的列表。 我正在嘗試使用此關聯節點名稱列表的 Inflate Spinner(它是我的 layout.xml 的一部分)。 因此,微調器將包含可供選擇的項目:A、B、C、D
我正在嘗試這樣:(只是一個片段)
@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);
}
});
}
運行此活動后,應用程序“崩潰”並出現錯誤。 如有必要,我還可以提供 LogCat 的摘錄。
期望:檢索與 Object1 關聯的節點列表,使用此列表填充微調器。
這是來自 Logcat 的報告:
(只是錯誤)
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)
您已經提供了活動的布局文件
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(MyActivity.this,R.layout.MyActivity, arrList);
ArrayAdapter<>() 的第二個參數需要 TextView 的資源 ID,如錯誤消息中所述
java.lang.IllegalStateException: ArrayAdapter 要求資源 ID 為 TextView
您可以使用 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.