简体   繁体   中英

Spinner Widget won't show the selected text

I am creating an android app that uses Cloud Firestore. There is a document in the database containing names that I need to show on the android app. The problem is that while the Spinner widget I am using is populating correctly, the widget does not show the selected text

I have already tried changing the background and text color of the spinner to no avail( 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 Code:

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


The Actual Output: Once value is selected from spinner, it shows nothing, no text appears

The Expected Behaviour: Once value from spinner is selected, the spinner should show the selected text

    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    spinner.setOnItemSelectedListener(this);

            // Spinner Drop down elements
           **Populate your List Here** 
     List<String> categories = new ArrayList<String>();
categories.add('');
            // Creating adapter for spinner
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

            // Drop down layout style - list view with radio button
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            // attaching data adapter to spinner
            spinner.setAdapter(dataAdapter);

      @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // On selecting a spinner item
            String item = parent.getItemAtPosition(position).toString();
     view.setSelection(int)
            // Showing selected spinner item
            Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }


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

Try changing the width and height of the spinner to wrap_content like this...

android:layout_width="wrap_content"

android:layout_height="wrap_content"

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.

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