简体   繁体   中英

Spinner selection won't populate edittext

I had a function that when a button was clicked a certain variable would enter the content of an edittext based on the selection of a spinner. it worked fine.

I am trying to do the same with onSelectedItem to populate the edittext without having to click a button but it isnt working atm

    Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


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

            String selection = String.valueOf(Spinner.getSelectedItem());
            if (selection.equals("test")) {
                editText3.setText("" + testvalue + "", TextView.BufferType.EDITABLE);
            } else if (selection.equals("dog")) {

                editText3.setText("" + dogvalue + "", TextView.BufferType.EDITABLE);
            } else if (selection.equals("cat")) {
                editText3.setText("" + catvalue + "", TextView.BufferType.EDITABLE);

            }
        }

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

        }
    });
    }

Are you sure you are using the object for your widget Spinner? Try changing it to:

    Spinner spinner=findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


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

        String selection = spinner.getSelectedItem().toString();
        if (selection.equals("test")) {
            editText3.setText("" + testvalue + "", TextView.BufferType.EDITABLE);
        } else if (selection.equals("dog")) {

            editText3.setText("" + dogvalue + "", TextView.BufferType.EDITABLE);
        } else if (selection.equals("cat")) {
            editText3.setText("" + catvalue + "", TextView.BufferType.EDITABLE);

        }
    }

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

    }
});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM