开发者

Android Spinner from Db - default value = null?

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 blan开发者_StackOverflow社区k record or anything?


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜