Reputation: 527
I want to make a spinner validation where if the user did not select any tuition centre name, it will toast a message. Then I want to fill the spinner with the data from firebase database. As refer in this link. I want to try this in my coding apps. But it didn't retrieve any data from my firebase database. Its been few days, I still can't figure out, can someone please help me....
My firebase database as show image below:-
Spinner coding:-
DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference();
mDatabaseRef.child("Advertisement").child(mAuth.getUid())
.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
//ArrayList<String> identity = new ArrayList<>();
final List<String> identity = new ArrayList<String>();
identity.add(0, "Choose Tuition Centre");
for (DataSnapshot snapshot : dataSnapshot.getChildren())
{
Advertisement advertisement = snapshot.getValue(Advertisement.class);
identity.add(advertisement.getAdstuitioname());
}
ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(RatingActivity.this, android.R.layout.simple_spinner_item, identity);
nameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mName.setAdapter(nameAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
mName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(position==0)
{
//Toast.makeText(getApplicationContext(),"No Item Selected",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position) +" Selected",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
My apps output interface:-
Upvotes: 0
Views: 200
Reputation: 764
In your code there are nested ids the actual data is after 2 ids. you need to traverse. Just replace the for loop's code to this code.
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
for (DataSnapshot dataSnapshot1 : snapshot.getChildren()) {
identity.add(dataSnapshot1.child("adstuitioname").getValue(String.class));
}
}
keep rest of as it is.
Upvotes: 1