How to set a default text to a Spinner

我是研究僧i 提交于 2019-12-03 00:19:32

Use this code

declaration

String selected, spinner_item;

spinner code

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            selected = sp.getSelectedItem().toString();
            if (!selected.equals("Country"))
                spinner_item = selected;
            System.out.println(selected);

            setid();
        }

        private void setid() {
            sp.setSelection(sp_position);
            t.setText(spinner_item);
        }

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

        }
    });
raman rayat
spinner.setPrompt("Pick One");

Use android:prompt="@string/select" in spinner....

Manishika

Do it this way. After setting the textView setSelection to 0th position of your spinner list

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {

        selected = sp.getSelectedItem().toString();
        System.out.println(selected);
        t.setText(selected);
        sp.setSelection(0);
    }
}

You only need 3 lines of code for this:

(1) Declare a global variable that is used to save and load the spinner item ID. By global, I mean declaring it right after "public class MainActivity extends Activity {".

int mySpinner_selectedId; 

(2) Then, add the following code AFTER the user has made his/her choice. One good location would be a button that appears together with your spinner.

mySpinner_selectedId = mySpinner.getSelectedItemPosition();

(3) Finally, add the following code right after "mySpinner.setAdapter(adapter);" to load the last selected item.

mySpinner.setSelection(mySpinner_selectedId);

You're most welcomed.

Try this way.

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        //selected = sp.getSelectedItem().toString();
        //System.out.println(selected);
        if(arg2!=0)
           t.setText(sp.getSelectedItem().toString());
        sp.setSelection(0);
    }
}

I hope this will help you. Let me know what happens. Thank you.

Try this:

    public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {

           spinner.setSelection(pos);

    }

try this

Countryspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            if (position == 0) {
                txtSpinner1.setHint("Select Country");
            } else {
                txtSpinner1.setText(CountryList.get(position));
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            txtSpinner1.setHint("Select Country");
        }

    });
jyomin

In onItemSelected method add this code sp.setSelection(0);

I done this in a simple way

  1. Add default text at adapter first or end position(in my case end ) and set this item as default selection. ---- Also get default selection position, So that no action will perform in setOnItemSelectedListener().

    taskName.add("one");
    taskName.add("two");
    taskName.add("select a task");
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, taskName);
    binding.spnTask.setAdapter(adapter);
    binding.spnTask.setSelection(adapter.getPosition("select a task name"));
    final int defaultPosition = binding.spnTask.getSelectedItemPosition();
    
  2. Check selected position is default text position

      binding.spnTask.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
            if (position == defaultPosition) {
                Toast.makeText(this, "No Action", Toast.LENGTH_SHORT).show();
            }else{  Toast.makeText(this, "Your Action" , Toast.LENGTH_SHORT).show();   }
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    });
    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!