Using childevent listeners worked just fine for populating my spinner from my Firebase real time database but it's not as effective as using firebaseUI. the documentation covers only recyclerview and listview, but I wanna use it for a spinner.
Query query = FirebaseDatabase.getInstance().getReference("Labs");
FirebaseListOptions<Labs> options = new FirebaseListOptions.Builder<Labs>().setQuery(query, Labs.class).build();
FirebaseListAdapter<Labs> firebaseListAdapter = new FirebaseListAdapter<Labs>(options) {
@Override
protected void populateView(@NonNull View v, @NonNull Labs model, int position) {
}
};
spinner.setAdapter(firebaseListAdapter);
I just don't know how to bind my model object with the view here.
There are two problems in your code. First, you are getting the following error:
layout cannot be null
Because you aren't assigning any layout in your populateView()
method. In your code, this method is empty. So you should create a custom layout that can contain a TeexView
for each element will help you solve this problem.
The second issue is that you are using FirebaseListAdapter
but don't listen for changes anywhere. To solve this, please use the following line of code in your onStart()
method:
firebaseListAdapter.startListening();
Don't also forget to remove it once is not needed by adding the following lines of code in your onStop()
method:
if(firebaseListAdapter != null) {
firebaseListAdapter.stopListening();
}