0

I want to create an app which would be having the capability of multiple account login. So the user would have to switch accounts from the drop down menu once there are more than one account. But I don't know how to add items to the drop down menu from my java class. I don't have any particular code for that right now because I don't know how that's going to work. I also wanted to know if there is any way by which we can edit the titles of the drop down menu items.

Matt
  • 464
  • 4
  • 13

2 Answers2

0

Items can be added to the spinner by providing spinner with an arraylist which would contain the items. For example in this example there are posts and each post has its own unique Id and the value event listener is able to read those ids and also read the children of the reference which is "placename" and it would add the area name to the arraylist every time it will run. And spinner can be provided an onitemselectedlistener to listen item select events. Hope that helped.

fDatabaseRoot.child("places").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> place = new ArrayList<String>();


for (DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
String placeName = areaSnapshot.child("placename").getValue(String.class);
        Place.add(placename);
    }

Spinner areaSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> newAdapter = new ArrayAdapter<String>(UAdminActivity.this, android.R.layout.simple_spinner_item, place);

 newAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    areaSpinner.setAdapter(newAdapter);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

I hope it's helps!

Regards.

Matt
  • 464
  • 4
  • 13
0

Android - Spinner (drop down menu)

Kishan Viramgama
  • 893
  • 1
  • 11
  • 23