1

So, at the moment I have a spinner which is populated from a database table. At the moment when using the spinner, the first value that is in the spinner is just the first from the database which isn't really ideal.

Is there a nice way for the spinner to start blank without having to insert a blank record or anything?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
user319940
  • 3,267
  • 8
  • 38
  • 53
  • Spinners always have a selected item, as far as I have seen. I have yet to see a blank Spinner if it's populated with items. Why do you want it empty? – Codemonkey Mar 17 '11 at 21:18
  • Yeah, I've had a rethink now and have decided to go down the route of making a an item for 'uncategorised' and have that as the first selection, rather than just blank. Thanks for the clarification guys. – user319940 Mar 17 '11 at 21:33
  • check http://stackoverflow.com/questions/5988605/how-to-remove-text-that-appears-on-a-spinner-control-in-android – Nick Aug 13 '11 at 22:46
  • possible duplicate of [How to make an Android Spinner with initial text "Select One"](http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one) – blahdiblah Mar 09 '13 at 11:43

3 Answers3

1

Well, you can use the attribute prompt of the Spinner view, whether android:prompt="whatever" or setPrompt("whatever") I hope this helps


Sorry, I misread, I don't know of any way of not showing an initial value without having to add an empty value. You will have to provide an item that corresponds to the "no choice" value.

raukodraug
  • 11,519
  • 4
  • 35
  • 37
1

So it looks like this isn't very possible, instead I've gone for a different method of adding a row at position 1 in the database with a name that I choose, and modified a query so that field can't be deleted.

user319940
  • 3,267
  • 8
  • 38
  • 53
0

If you want, there is a decorater spinnerAdapter witch add automatically a default value :



    protected class SpinnerAdapterWithNoValue implements SpinnerAdapter {

            private SpinnerAdapter _current;
            private final static String defaultValue = "Choisir";

            public SpinnerAdapterWithNoValue(SpinnerAdapter base) {
                _current = base;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return _current.getCount()+1;
            }

            @Override
            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                if(arg0 == 0 || arg0 == -1)
                {
                    return null;
                }
                return _current.getItem(arg0-1);
            }

            @Override
            public long getItemId(int arg0) {
                // TODO Auto-generated method stub
                if(arg0 == 0 || arg0 == -1)
                {
                    return -1;
                }
                return _current.getItemId(arg0-1);
            }

            @Override
            public int getItemViewType(int arg0) {
                // TODO Auto-generated method stub
                if(arg0 == 0 || arg0 == -1)
                {
                    return -1;
                }
                return _current.getItemViewType(arg0-1);
            }

            @Override
            public View getView(int arg0, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                if(arg0 == 0 || arg0 == -1)
                {
                    final TextView v = (TextView) ((LayoutInflater)getContext()
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                            .inflate(R.layout.spinner_text,parent,false);
                    v.setText(defaultValue);
                    return v;
                }
                return _current.getView(arg0-1, convertView, parent);
            }

            @Override
            public int getViewTypeCount() {
                // TODO Auto-generated method stub
                return _current.getViewTypeCount();
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return _current.hasStableIds();
            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                return _current.isEmpty();
            }

            @Override
            public void registerDataSetObserver(DataSetObserver arg0) {
                // TODO Auto-generated method stub
                _current.registerDataSetObserver(arg0);
            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver arg0) {
                // TODO Auto-generated method stub
                _current.unregisterDataSetObserver(arg0);
            }

            @Override
            public View getDropDownView(int arg0, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                if(arg0 == 0 || arg0 == -1)
                {
                    CheckedTextView v = (CheckedTextView)((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(android.R.layout.simple_spinner_dropdown_item,parent,false);
                    v.setText(defaultValue);
                    return v;
                }
                return _current.getDropDownView(arg0-1, convertView, parent);
            }
        }


Then you can create your own spinner using this decorater :




     public class SpinnerWithNoValue extends Spinner {

        public SpinnerWithNoValue(Context context) {
            super(context);
        }

        public SpinnerWithNoValue(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public SpinnerWithNoValue(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        @Override
        public void setAdapter(SpinnerAdapter orig ) {
            final SpinnerAdapter adapter = new SpinnerAdapterWithNoValue(orig);
            super.setAdapter(adapter);

            try {
                final Method m = AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt",int.class);
                m.setAccessible(true);
                m.invoke(this,-1);

                final Method n = AdapterView.class.getDeclaredMethod("setSelectedPositionInt",int.class);
                n.setAccessible(true);
                n.invoke(this,-1);

            } catch( Exception e ) {
                throw new RuntimeException(e);
            }
        }

        /*
         * getSelectedItem
         * renvoi null si la valeur par defaut est séléctionnée
         * @see android.widget.AdapterView#getSelectedItem()
         */
        @Override
        public Object getSelectedItem()
        {
            return super.getSelectedItem();
        }
        }


You just have to change the spinner declaration in your xml layout :



    com.myproject.SpinnerWithNoValue


If you want, you can change the code to set the default text in the tag of your spinner.

If the selected value is the default value, getItem will return null and getItemId will return -1