example of how to popular a spinner with firebase data

Asked

Viewed 1,166 times

0

I’m having trouble popular a spinner with firebase data...

is as follows:

I have an "Activity room" with registered rooms and listed in a listview, saved in firebase... I created a new "Activity schedule" with a spinner component that is to get populated by those rooms that are in the "Activity room", only that I have no idea how to do that, ie, pass data from one Activity to another and take this data and popular in spinner. You could help me?

  • Load the rooms into a database as Arraylist is already getting?

  • that I have already managed... the doubt I have and for example I can load the rooms in the "Activity rooms" where I can already edit, delete and insert in this list. " i was wondering if in the "Scheduling Activity" in the Spinner component I have to create a new Arraylist or call that Arraylist of rooms that is already created?

  • Activity Scheduling you access through Activity Salas? If you do not create another Arrylist. You can leave the list in memory, but I don’t think it’s worth.

1 answer

0

Try this:

fDatabaseRoot.child("areas").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Is better to use a List, because you don't know the size 
        // of the iterator returned by dataSnapshot.getChildren() to
        // initialize the array
        final List<String> areas = new ArrayList<String>();

        for (DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
            String areaName = areaSnapshot.child("areaName").getValue(String.class);
            areas.add(areaName);
        }

        Spinner areaSpinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(UAdminActivity.this, android.R.layout.simple_spinner_item, areas);
        areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        areaSpinner.setAdapter(areasAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Response link: https://stackoverflow.com/a/38493214/5453684

Browser other questions tagged

You are not signed in. Login or sign up in order to post.