Reputation: 1006
Let's say I have the following firebase realtime database structure:
{
"Object1" : {
"A" : "exampleData",
"B" : "exampleData",
"C" : "exampleData",
"D" : "exampleData"
}
}
I'm trying to get a list of associated node names from "Object1". I'm trying Inflate Spinner (which is part of my layout.xml) with this list of associated node names. The spinner will therefore contain items to choose from: 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.
Expectation: Retrieve a list of nodes associated with Object1, populate the spinner with this list.
Here is a report from Logcat:
(just errors)
E/RunLoop: Uncaught exception in Firebase Database runloop (3.0.0). Please report to [email protected]
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)
Upvotes: 0
Views: 41
Reputation: 346
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
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
You can use R.layout.support_simple_spinner_dropdown_item instead as below
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(MyActivity.this,R.layout.support_simple_spinner_dropdown_item,arrList);
Upvotes: 1