简体   繁体   English

Android 微调器未显示所选文本

[英]Android Spinner not showing selected text

I am pulling data from a firestore database into a ArrayList and then loading those objects into a spinner.我将数据从 Firestore 数据库中提取到 ArrayList 中,然后将这些对象加载到微调器中。 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.当我打开微调器进行选择时,所有对象都会正确显示,但是当您从微调器中打开 select 和 object 时,微调器保持空白并且不显示任何文本。 I have already tried adding adapter.notifyDataSetChanged();我已经尝试添加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:这是我的 java 文件:

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:这是 xml 文件:

<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每当从 Firebase 完全接收到用户时更新您的微调器,因此updateSpinner()的调用转移到usersRef.get().addOnSuccessListener并将其从onCreate()方法中删除

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();
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM