Spinner does not show selected value

感情迁移 提交于 2019-11-27 14:58:25

This answer may be a little stupid but try it. It worked for me.

  1. Check the background color of your spinner!
  2. And if it`s white change it
  3. Enjoy it!

I got same problem and solved by adding notifyDataSetChanged() after binding data in Spinner.

First of all I have bind adapter with Blank ArrayList then getting List of Items from Server and added to that List but forgot to notifyDataSetChanged() after updated List.

just add adapter.notifyDataSetChanged(); after updating list.

Hope it will helpful.

TastyCatFood

Problem:

Spinner displays neither the default nor selected item value. But drop-down menu items are show when selected.

Cause:

Background and text color both being white!!!

Solutions:

xml(Preferable):

Write a custom layout for a spiner item and use it instead of the default,android.R.layout.simple_spinner_item.

How to change spinner text size and text color?

Code(Less reliable):

your_spinner_instance.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
                               long id) {
        ((TextView) view).setTextColor(Color.RED);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }

});

Android needs some major update or maybe dart and flutter should take over...

Thanks Catluc

if you have a custom adapter you should change the TextView text color

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView view = (TextView) super.getView(position, convertView, parent);
    view.setTextColor(Color.parseColor("#000000"));
    return view;
}

and if you don't have a custom adapter you should just change spinner background

Mladen Rakonjac

Use wrap_content for the height of Spinner.

Probably it does not have enough height to show text.

Vyshak Athreya

Well this also happens if the context is not properly given. I was using getApplicationContext() where as it needs getBaseContext().

You should use the first Spinner to get the values.

Try the following code:

String provider = spinner1.getSelectedItem().toString();

What you are doing is getting the default value of the spinner.

Check these links:

  1. http://www.technotalkative.com/android-spinner-example/
  2. http://www.mkyong.com/android/android-spinner-drop-down-list-example/

Else, try to store it on selecting the item:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapter, View v,
                    int position, long id) {
                // On selecting a spinner item
                String item = adapter.getItemAtPosition(position).toString();

                // Showing selected spinner item
                Toast.makeText(getApplicationContext(),
                        "Selected Country : " + item, Toast.LENGTH_LONG).show();
            }

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

            }
        });

use android:spinnerMode="dropdown" attribute in your declared Spinner element's xml

Try this.

 final ArrayList<String> providerlist= new ArrayList<String>();
    Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            // On selecting a spinner item
            String item = providerlist.get(position);

            // Showing selected spinner item
            Toast.makeText(this,
                    "Selected Country : " + item, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

Generally spinners gets selected by default when they are used ,so try to set blank,or any other data to first position that is zero position, you will get exact position of you selected item .

The issue from what I found was with the style sheet.Use this

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->
  <item name="windowNoTitle">false</item>
  <item name="windowActionBar">true</item>
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
</style> 

For the xml layout use this

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    android:paddingBottom="5dp"
    style="@style/AppTheme">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Spinner"
            android:layout_marginTop="10dp"
            android:textColor="@color/colorBlack"/>

        <Spinner
            android:id="@+id/Spinner"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:backgroundTint="@color/colorPrimary"
            android:textColorHint="#05ab9a"
            android:padding="15dp"
            style="@style/Base.Widget.AppCompat.Spinner.Underlined"
            tools:targetApi="lollipop" />
    </LinearLayout>
</ScrollView>

And finally for the class

String [] NUMBERS= {"3 ","6 ","13 "};

Spinner s_spinner = (Spinner) findViewById(R.id.Spinner);

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_dropdown_item_1line, NUMBERS);

// Specify the layout to use when the list of choices appears 
           spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
s_spinner.setAdapter(spinnerAdapter );

This answer may be a little stupid but if you do the same mistake then try... set values to your ArrayList first and then assign that arrayList to spinner. I declared a global arrayList and set it to spinner first and then add values to it from another method... at that time i faced the same problem. Otherwise you can do notifyDataSetChanged() to your arrayList.

Abhijit Rajmane

try this code==>

ArrayAdapter<String> stateNameAdaptor = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stateNameList);  
     stateNameAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spnState.setAdapter(stateNameAdaptor);

For me the problem is that I'm using getApplicationContext(). When I change to this it works fine.

ArrayAdapter<*> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, documentsCategories);

Wierd. I had the same problem. What I did to make it work : 1. Add some initial data to the list ( ex.--- please select - -) 2. Load the rest of data and add it to the list 3. Call adapter.notifyDatasetChaged()

JAMES MILLER

I just had this problem and after trying all of the solutions listed found out that the issue was that I had set the spinner layout_width to 60dp.

Changed it to fill_parent and problem solved.

Try This code

ArrayAdapter<String> arrayAdapte=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,states);
    arrayAdapte.setDropDownViewResource(android.R.layout.simple_list_item_1 );
    spinnerState.setAdapter(arrayAdapte);

    String data=spinnerState.getSelectedItem().toString(); // this is select particular item from list;

    int position=spinnerState.getSelectedItemPosition(); // this return position of data selected in list; 

You have to do some changes as:

Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
 ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

instead of "android.R.layout.simple_spinner_dropdown_item"

adapter1.setDropDownViewResource(android.R.layout.simple_list_item_activated_1);

Use "android.R.layout.simple_list_item_activated_1"

spinner1.setAdapter(adapter1);

It will highlight the selected value accordingly. Selected color is chosen by your apps default theme.

For more information follow below link: https://developer.android.com/reference/android/R.layout.html#simple_list_item_activated_1

out_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="50dp"
            android:gravity="center"
            android:orientation="vertical">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/txtProgress"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:progress="50"
                android:progressTint="@android:color/black" />

            <TextView
                android:id="@+id/txtProgress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Progress"
                android:textColor="@android:color/black" />
        </LinearLayout>  

Java
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!