Spinner does not display the selected value

I have applied a spinner filling a list of arrays through the database. I can get and show a list of arrays in my spinner array adapter, but if I select an item in the spinner it won't show in the spinner? That I was wrong here?

Here is my code,

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

      

I am getting the selected row of an item using this,

Spinner provid = (Spinner)findViewById(R.id.prospin);
String provider =provid.getSelectedItem().toString();

      

Can anyone help me with pls !!!

+23


source to share


20 replies


This answer may be a little silly, but try it. This worked for me.



  • Check the background color of your counter!
  • And if it's white, change it
  • Enjoy it!
+42


source


I got the same problem and solved it by adding notifyDataSetChanged()

after data binding in Spinner.

First of all I have an empty list bind adapter ArrayList

then I get the list of items from the server and add it to this list but forgot notifyDataSetChanged()

after updating the list.



just add adapter.notifyDataSetChanged();

after updating the list.

Hope this will be helpful.

+13


source


Problem:

Spinner does not display default or selected value. But dropdown menu items are displayed when selected.

Cause:

The background and text color are both white.

Decision:

XML (Preferred):

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

.

How do I change the font 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 carry over ...

Thanks Catluc

+10


source


Use wrap_content

for height Spinner

.

It may not have enough height to display the text.

+3


source


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

@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 just need to change the background of the spinner

+3


source


Ok, this also happens if the context is not set properly. I have used getApplicationContext()

where needed getBaseContext()

.

+2


source


Check out these links:

Please try again to save it on item selection:

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

            }
        });

      

0


source


use attribute android:spinnerMode="dropdown"

on declared element Spinner

xml

0


source


Try it.

 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) {

        }
    });

      

0


source


They are usually selected by default when used, so try to set empty or any other data to the first position, which is zero, you will get the exact position of the item you selected.

0


source


The problem with what I found is with the stylesheet. 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 layout xml 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 );

      

0


source


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

      

0


source


This answer might be a little silly, but if you make the same mistake try ... set the values ​​to your ArrayList first, and then assign that arrayList to rotate. I declared a global array list and set it first and then added values ​​to it from another method ... at that time I ran into the same problem. Otherwise, you can do notifyDataSetChanged () on your arrayList.

0


source


For me, the problem is what I was using getApplicationContext()

. When I replace getApplicationContext()

with this

, it works fine.

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

      

0


source


Wierd. I have the same problem. What I did to get it working: 1. Add some initial data to the list (eg ---, please select - -) 2. Load the remaining data and add it to the list 3. Call adapter.notifyDatasetChaged ()

0


source


I just had this problem and after trying all the solutions listed, it turned out that the problem was that I set the spinner layout_width

to 60dp .

Changed it to fill_parent and the problem is solved.

0


source


You must use the first Spinner to get the values.

Try using the following code:

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

      

What you do is get the default value for the counter.

-1


source


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; 

      

-1


source


You have to make some changes like:

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 display the selected value appropriately. The selected color is the default for your applications.

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

-1


source


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

      

-1


source







All Articles