简体   繁体   中英

How do I get the data of a returned value from a spinner?

I am making an android application that asks a user to enter the name of a code and then the actual code itself. Once the user inputs these values and presses a submit button the inputs are passed to another class that stores them. For this so far I have used (with the unnecessary bits in between omitted):

EditText nameCode, dataCode;

nameCode = (EditText) findViewById(R.id.codeName);                                      
dataCode = (EditText) findViewById(R.id.codeData);

spinner = (Spinner) findViewById(R.id.ticketType);                                                                                  
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.codeType, android.R.layout.simple_spinner_dropdown_item);      
spinner.setAdapter(adapter);

final Button addBtn = (Button) findViewById(R.id.submitbtn);                           
addBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        addPassCode(nameCode.getText().toString(),dataCode.getText().toString());      
        populateCodes();                                                                
    }
});

private void addPassCode(String name, String passCode) {
    PassCodes.add(new PassCode(name, passCode));

This part all works fine. What I am trying to do is now pass another variable as well as 'name' and 'passCode' in my addPassCode method. I have a spinner that contains an array of 3 values (Strings) and I want the user to select a value and then the equivalent of getText().toString() for a spinner input so I can add it to my method to pass it to my class 'PassCodes' storing all the data. Also how would i cast this variable similarly to the previous 2 to EditText?

Also what would be the correct variable type for ticketType?

You can simply do the following:

String text = spinner.getSelectedItem().toString();

And then to set the value in your EditText, simply do:

dataCode.setText(text);

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