简体   繁体   中英

If condition on Spinner drop-down list

I made a drop-down list using spinner but now I want and a specific message to display when a certain value is selected along with an EditText when a certain keyword is entered. I attempted using if condition but it didn't work. The scenario is like this: When the user input their age over 18 and selects "Yes" from the drop-down hire , a certain message will appear, both conditions must be satisfied

 public void stan(View hiring){
        ArrayAdapter<CharSequence> hire=ArrayAdapter.createFromResource(this,R.array.decide, android.R.layout.simple_spinner_item);
        hire.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(hire);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String decide =spinner.getSelectedItem().toString()
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        int age= Integer.parseInt(aged.getText().toString());
        if(age>18 && spinner.equals("Yes")){
            Toast.makeText(this,"Ok, go eat cabbage sauce",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this,"no",Toast.LENGTH_LONG).show();
        }

Try this

public void stan(View hiring){
        ArrayAdapter<CharSequence> hire=ArrayAdapter.createFromResource(this,R.array.decide, android.R.layout.simple_spinner_item);
        hire.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(hire);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String decide =spinner.getSelectedItem().toString();
                int age= Integer.parseInt(aged.getText().toString());
                if(age>18 && decide.equals("Yes")){
                Toast.makeText(this,"Ok, go eat cabbage sauce",Toast.LENGTH_LONG).show();
                }else{
                Toast.makeText(this,"no",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

Use spinner.getSelectedItem().toString()

Finally, Your condition will be

if(age>18 && spinner.getSelectedItem().toString().equals("Yes")){
             Toast.makeText(YourActivityName.this,"Ok, go eat cabbage sauce",Toast.LENGTH_LONG).show();
            }else{
             Toast.makeText(YourActivityName.this,"no",Toast.LENGTH_LONG).show();
            }

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