public class MainActivity extends AppCompatActivity {
DatabaseReference reference;
Spinner areaSpinner;
ArrayList<String> areas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://vbspinner.firebaseio.com/Location");
areaSpinner = (Spinner) findViewById(R.id.spinner);
DatabaseReference mref = reference.child("areas");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(this, String.class, android.R.layout.simple_spinner_item, mref) {
@Override
protected void populateView(View v, String model, int position) {
((TextView)findViewById(android.R.id.text1)).setText(model);
}
};
areaSpinner.setAdapter(firebaseListAdapter);
}
My Firebase database:
My constructor class code is
public class VBSpinner extends Application {
@Override
public void onCreate() {
super.onCreate();
if(!com.google.firebase.FirebaseApp.getApps(this).isEmpty()){
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
I would like that my spinner values are from the Firebase database (Kvp
, Tn
,Tvl
, so on). Where do I need to change the data?
Maybe I am quite late to answer but try my method to parse firebase document items into spinner adapter
https://gist.github.com/majeytunio/9cc24a7edb06b2c2d3bbbb649ac9b602
In above code, you make
reference
object point to"Location"
(which I think what you're trying to do. But in the following code:You make another reference called
mref
that (because ofchild("areas")
) point to"https://vbspinner.firebaseio.com/Location/areas"
. So if you usemref
in yourFirebaseListAdapter
, it will return null because there is no value in".../Location/areas"
.You should use
reference
inFirebaseListAdapter
to make it work.