微调小部件将不会显示所选文本

时间:2019-08-07 10:29:11

标签: java android firebase google-cloud-firestore android-spinner

我正在创建一个使用Cloud Firestore的android应用。数据库中有一个文档,其中包含我需要在android应用上显示的名称。问题是当我正在使用的Spinner小部件正确填充时,该小部件不会显示所选文本

我已经尝试将微调器的背景和文本颜色更改为无效(Spinner does not show selected value

Spinner XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="168dp"
        android:layout_marginBottom="416dp"
        android:onClick="loginButton"
        android:text="Login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="291dp"
        android:layout_height="55dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="304dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

Java代码:

public class MainActivity extends AppCompatActivity{
    List<String> names;
    Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = findViewById(R.id.spinner);
        names = new ArrayList<String>();
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("Investors").document("InvestorNames").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                Map<String, Object> map = documentSnapshot.getData();
                for(Map.Entry<String, Object> entry: map.entrySet()){
                    names.add((String)entry.getValue());
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
    }


实际输出: 从微调器中选择了值后,它什么也不显示,没有文本出现

预期的行为: 选择了微调框的值后,微调框应显示所选文本

3 个答案:

答案 0 :(得分:0)

async function GetData() {
    for (var i = 1; i <= 5; i++) {
        try {
            var data = await this.sheetsService.spreadsheets.values.get({
                spreadsheetId,
                range
            });
            return data;
        } catch (err) {
            if (err.code === 429) {
                console.log("Too many requests, try again in 100sek");
                await sleep(100000); //sleep function with 100sek
                continue;
            }
        }
    }
}

答案 1 :(得分:0)

     ArrayList<String> routes = new ArrayList<String>();
        // routes filled with values at runtime
        ...
        ArrayAdapter<String> aa = new ArrayAdapter<String>(GOFdroid.this, android.R.layout.simple_spinner_item, routes);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner destSpinner = (Spinner) dialogView.findViewById(R.id.edit_event_destination);

        String dest = events.get(pos).getDestination();
        int routesPos = routes.indexOf(dest);
        Log.d(TAG, "Dest: " + dest + ", pos: " + routesPos);


        destSpinner.setAdapter(aa);

destSpinner.setSelection(routesPos);

答案 2 :(得分:0)

尝试像这样将微调器的page 1width更改为height ...

wrap_content