Veerabahu Chokkalingam Veerabahu Chokkalingam - 26 days ago 5
Android Question

Populatate the spinner from Firebase database

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:

1

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?

Answer Source
reference = FirebaseDatabase.getInstance()
     .getReferenceFromUrl("https://vbspinner.firebaseio.com/Location");

In above code, you make reference object point to "Location" (which I think what you're trying to do. But in the following code:

DatabaseReference mref = reference.child("areas");

You make another reference called mref that (because of child("areas")) point to "https://vbspinner.firebaseio.com/Location/areas". So if you use mref in your FirebaseListAdapter, it will return null because there is no value in ".../Location/areas".

You should use reference in FirebaseListAdapter to make it work.