Android spinner set default text

后端 未结 3 679
迷失自我
迷失自我 2021-01-12 05:15

I\'ve implemented a spinner into my app. I\'m showing a list of schools but I want a default value/prompt so the user knows to select a school. I also want to do this withou

相关标签:
3条回答
  • 2021-01-12 05:49

    You should create custom ArrayAdapter with a String for your desired text in its constructor. Then, you should switch the first element of your array with your default text, and after the user click on the spinner it should switch back to the original array.

    Example:

    Activity class:

        public class SpinnerActivity extends Activity {
    
        Spinner spinner;
        String defaultTextForSpinner = "Your deafult text here";
        String[] arrayForSpinner = {"One", "Two", "Three"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_spinner);
    
            spinner = (Spinner) findViewById(R.id.your_spinner);
            spinner.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, arrayForSpinner, defaultTextForSpinner));
        }
        }
    

    CustomSpinnerAdapter class:

        public class CustomSpinnerAdapter extends ArrayAdapter<String>{
    
        Context context;
        String[] objects;
        String firstElement;
        boolean isFirstTime;
    
        public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, String defaultText) {
            super(context, textViewResourceId, objects);
            this.context = context;
            this.objects = objects;
            this.isFirstTime = true;
            setDefaultText(defaultText);
        }
    
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            if(isFirstTime) {
                objects[0] = firstElement;
                isFirstTime = false;
            }
            return getCustomView(position, convertView, parent);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) { 
            notifyDataSetChanged();
            return getCustomView(position, convertView, parent);
        }
    
        public void setDefaultText(String defaultText) {
            this.firstElement = objects[0];
            objects[0] = defaultText;
        }
    
        public View getCustomView(int position, View convertView, ViewGroup parent) {
    
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.spinner_row, parent, false);
            TextView label = (TextView) row.findViewById(R.id.spinner_text);
            label.setText(objects[position]);
    
            return row;
        }
    
        }
    
    0 讨论(0)
  • 2021-01-12 06:03

    Try setSelection method to set default text:

    spinner.setSelection(adapter.getPosition(DEFAULT_CURRENCY_TYPE))

    And look this link: How to make an Android Spinner with initial text "Select One". It is very useful (@aaronvargas answer is what you need).

    0 讨论(0)
  • 2021-01-12 06:15
           @Override
        protected void onPostExecute(PositionBean[] result) {
            String[] spinnerArray = new String[result.length];
    
            if(result!=null) {
    
                for (int i = 0; i < result.length; i++) {
    
                    String positionName = result[i].getPositionName();
                    spinnerArray[i] = result[i].getPositionName();
    
                }
                ArrayList<String> positionList = new ArrayList<String>(Arrays.asList(spinnerArray));
                positionList.add(0,"select");
                String [] positions = positionList.toArray(new String[positionList.size()]);
    
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineActivity.this, android.R.layout.simple_spinner_item,
                        positions);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                positionSpinner.setAdapter(adapter);
            }else{
                Log.d("NULL", "This is null");
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题