3

Appreciate some help here on a spinner issue I'm getting.

The list appears fine when clicking on the drop-down arrow. However, when clicking on the selection, the spinner view still appears as blank. The selection's text does not appear. What gives?

On Android Studio's preview, it appears fine from my assigned android:entries. Screenshot here: (https://i.stack.imgur.com/tsIVV.jpg)

As you can see, the background is grey, and everything else is white background as well. So I don't think the color is the issue here.

I've checked and changed background colors, and even removed some widget so that I can see what if anything was blocking the selection to appear.

Is there something aside from the normal declaration of Spinner, Arraylist, creating a new arrayadapter, setDropDwonViewResource, setting the arrayadapter to the spinner that I need to do?

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mStoreSpinner.setAdapter(adapter);

The XML for the spinner is also as "simple" as can be:

<Spinner
    android:id="@+id/s_spinner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/recyclerord"
    app:layout_constraintLeft_toRightOf="@+id/orderID"
    app:layout_constraintTop_toBottomOf="@+id/header"
    app:layout_constraintRight_toRightOf="@+id/ConstraintLayout"
    android:visibility="visible"
    android:layout_marginStart="0dp"
    android:entries="@array/array_test"
    >
</Spinner>

Thank you.

Edited: This is what I've added.

mStoreSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int postion, long arg3) {
            // TODO Auto-generated method stub
            String spinnerValue= parent.getItemAtPosition(postion).toString();
            Log.d(TAG, "test");
            Toast.makeText(getBaseContext(), "Selected item" + spinnerValue, Toast.LENGTH_SHORT).show();
        }

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

        }
    });
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mStoreSpinner.setAdapter(adapter);
H Wong
  • 331
  • 3
  • 16
  • I have not implemented the onClickListeners yet - do they need to be there before the spinner will work fine? – H Wong Mar 12 '18 at 09:29

4 Answers4

3

I have not implemented the onClickListeners yet - do they need to be there before the spinner will work fine? I guess yes.

Add a setOnItemSelectedListener to your Spinner like this :

mStoreSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v,
                int postion, long arg3) {
            // TODO Auto-generated method stub
        String  spinnerValue= parent.getItemAtPosition(postion).toString();

            Toast.makeText(getBaseContext(),
                    "Selected item" + spinnerValue,
                    Toast.LENGTH_SHORT).show();

        }

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

        }
    });

Also feel free to see this tutorial to understand it a little bit more

EDIT

You should follow steps :

Declare your Spinner

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

Create the ArrayAdapter

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
        spinerArray, android.R.layout.simple_spinner_item);

Set the DropDown

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Set the adapter

spinner.setAdapter(adapter);

There are two ways to implement the setOnItemSelectedListener()

  1. Implementing its interface : implements OnItemSelectedListener
  2. Using setOnItemSelectedListener(new OnItemSelectedListener() {...}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • I just added this, but it still doesn't work. Actually, the toast also doesn't appear. – H Wong Mar 12 '18 at 09:37
  • Edit your question with your code that you've implemented, to see if your put it on the right place or not – Skizo-ozᴉʞS ツ Mar 12 '18 at 09:38
  • @HWong you have to put the setOnItemSelectedListener below the setAdapter – Skizo-ozᴉʞS ツ Mar 12 '18 at 09:49
  • Yes, actually I did that too before I changed it. – H Wong Mar 12 '18 at 09:57
  • I did it both ways, but still nada – H Wong Mar 12 '18 at 09:58
  • 1
    Actually - thanks! I think that worked. I also changed my "spinnerArray.add" (it was grabbing data from Firestore) - it might been some combination of unsanitized String Firestore as well. – H Wong Mar 12 '18 at 10:01
  • 1
    Actually, just to add to this, I think I might be having the same issue as https://stackoverflow.com/questions/48755988/firebase-firestore-spinner-hack but there is no app crashes or error as the original poster. In my case, the selected selection did not appear in the spinner. Once I add a simple string to the array beforehand, then it works. – H Wong Mar 12 '18 at 10:09
  • For me, I was missing notifyDataSetChanged call on adapter. – SloCompTech May 18 '21 at 21:47
  • @SloCompTech yes it happens :) if I helped you mark this answer with an upvote – Skizo-ozᴉʞS ツ May 19 '21 at 10:26
0

This is working code, may this helps you:

<Spinner
  android:id="@+id/spinner"
  style="?android:attr/textViewStyle"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:spinnerMode="dialog"
  android:textAppearance="?android:attr/textAppearanceMedium"
  android:textColor="@color/colorBlack"
  android:textColorHint="@color/colorGray"
  android:textSize="@dimen/_14sdp" />

Make custom R.layout.list_item

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/_4sdp"
android:paddingLeft="@dimen/_14sdp"
android:paddingRight="@dimen/_14sdp"
android:paddingTop="@dimen/_4sdp"
android:text="Test"
android:textColor="@color/colorGray"
android:textSize="@dimen/_14sdp" />

Set adapter like this:

SpinnerAdapter adapter = new SpinnerAdapter(mActivity, R.layout.list_item,
            android.R.id.text1, yourListHere);
    spinner.setAdapter(adapter);

SpinnerAdapter code:

public class SpinnerAdapter extends ArrayAdapter {

    public SpinnerAdapter(@NonNull Context context, @LayoutRes int resource, @IdRes int textViewResourceId, @NonNull Object[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public SpinnerAdapter(@NonNull Context context, @LayoutRes int resource, @IdRes int textViewResourceId, @NonNull List objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
        return view;
    }
}
Viks
  • 1,510
  • 4
  • 22
  • 50
  • Any examples of using an ArrayList instead? Looks quite similar from what I have except for an ArrayList. – H Wong Mar 12 '18 at 09:59
  • see an updated answer with a custom adapter in which you can use Object[] or List – Viks Mar 12 '18 at 10:12
0

first of remove in xml in spinner control in this line android:entries="@array/array_test" because if you are passing in adapter in list then already spinner control containing arraylist there for remove it and used below code ...

 List<String> spinnerArray=new ArrayList<>(); // hear you can add in any array.
    spinnerArray.add("Color");
    spinnerArray.add("abd");
    spinnerArray.add("cde");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        }

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

        }
    });
  • This answer is not corect! This line need edit ` ArrayAdapter adapter = new `ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);` . The ArrayAdapter argument 3 must be a resource not an array! – Jose Mhlanga Jul 12 '20 at 20:14
0

I had same problem. As per my experience with this: If we create ArrayList suppose of String type and we used ArrayAdapter to Bind list. Then Please make sure you have converted your ArrayList to String Array.

ArrayAdapter<String> yourAdapter = 
new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, 
yourArrayList.toArray(new String[yourArrayList.size()]));

This works!

amit bansode
  • 339
  • 3
  • 14