In my case the list is populated from firestore before supplied to the adapter construction. The list is already populated and is displayed in spinner. but the size of the list was returned zero. So I only can select the item from the list, but there is no display in the spinner text and unable to call the onitemselected part. I previously made a callback of the querysnapshot and from the snapshot I populated the list. it caused only adding the items to the spinner but cannot be selected. I resolved this by taking a callback of the list instead of the querysnapshot from a repository class and after this my list is populated well and its size is correct and the spinner works fine..my code is attached here.. hope this helps someone...
//interface
public interface SprRbaCallback{
void onSprRbaCallback(List<String> list);
}
//Repository class
public void getRBA(final SprRbaCallback sprRbaCallback){
rbaCollection.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(DocumentSnapshot snapshot : queryDocumentSnapshots){
String item = (String) snapshot.get("dealerName");
assert item != null;
Log.d(TAG, item);
list.add(item);
}
Log.d(TAG, String.valueOf(list.size()));
sprRbaCallback.onSprRbaCallback(list);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, e.toString());
}
});
}
// calling method..
private void addItemsToRBASpinner() {
firebaseRepository.getRBA(new FirebaseRepository.SprRbaCallback() {
@Override
public void onSprRbaCallback(List<String> list) {
int size = list.size();
Log.d(TAG + "listsize", String.valueOf(size));
ArrayAdapter<String> sprAdaptSelectRBA = new ArrayAdapter<String>(CreateAccountActivity.this,
R.layout.spinneritem, list);
sprAdaptSelectRBA.setDropDownViewResource(R.layout.rbaspinner_dropdown);
spr_rbaSelect.setAdapter(sprAdaptSelectRBA);
sprAdaptSelectRBA.notifyDataSetChanged();
spr_rbaSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("NOBY", "ENTERED ON ITEMSELETED");
txt_selectedRBA = (String) parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(), "Selected RBA: " + txt_selectedRBA, Toast.LENGTH_LONG).show();
Log.d("NOBY", txt_selectedRBA);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("NOBY", "ENTERED NOTHING SELECTED");
txt_selectedRBA = "Dixon";
}
});
}
});
}
This code works fine for me...hope this help someone...:)