I am pulling data from a firestore database into a ArrayList and then loading those objects into a spinner. When I open the spinner to make a selection all the objects show up properly but when you select a object from the spinner the spinner remains blank and dose not show any text. I have already tried adding adapter.notifyDataSetChanged();
and I have also tried to change the color of the text as I have seen this suggested elsewhere but neither solution worked.
Here is my java file:
public class AdminEditor extends AppCompatActivity {
FirebaseFirestore db;
CollectionReference usersRef;
ArrayList<UserItem> users;
Spinner spinnerUser;
Button btnGetData;
TextView txtTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_editor);
//Set up FireStore and references
db = FirebaseFirestore.getInstance();
usersRef = db.collection("users");
//Set up resources
txtTest = findViewById(R.id.txtTest);
spinnerUser = findViewById(R.id.spinnerUser);
btnGetData = findViewById(R.id.btnGetData);
users = new ArrayList<UserItem>();
loadUsers();
updateSpinner();
}
public void loadUsers() {
usersRef.get()//Get entire users collection
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
users.add((UserItem)documentSnapshot.toObject(UserItem.class));//Add each user from the collection to the arraylist
}
}
});
}
//To fill spinners with data
public void updateSpinner() {
ArrayAdapter<UserItem> adapter = new ArrayAdapter<UserItem>(this, android.R.layout.simple_spinner_item, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerUser.setAdapter(adapter);
spinnerUser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
UserItem user = (UserItem) parent.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
Here is the xml file:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AdminEditor">
<Spinner
android:id="@+id/spinnerUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="128dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnGetData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Get Data"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinnerUser" />
<TextView
android:id="@+id/txtTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGetData" />
</androidx.constraintlayout.widget.ConstraintLayout>```
Thanks for your help
update your spinner whenever the users are entirely received from the Firebase, so transfer the call to updateSpinner()
into usersRef.get().addOnSuccessListener
and remove it from onCreate()
method
public void loadUsers() {
usersRef.get()//Get entire users collection
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
users.add((UserItem)documentSnapshot.toObject(UserItem.class));//Add each user from the collection to the arraylist
}
updateSpinner();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.