开发者

I want to use an if statement with a spinner item but without an OnItemSelectedListener

What I'm trying to do is use an if statement to determine which item in my spinner is selected and then from there allow two different radio buttons to be selected and depending on which one do something different. The output for a TextView will change based on which Spinner item and which radio button is selected. I'm not having any problems with the radio buttons and the output will vary based on which one is selected but the spinner does not do anything. I can select any item and it will have the same output and ignore the first if statement regarding the spinner and just use the second.I thought it worked and moved on to the rest of my app then went back and tested it and now im back to square one. I don't want to use a OnItemSelectedListener but I realize that Im probably trying to compare the item to the spinner the wrong way. For an OnItemSelectedListener I would use:

if (parent.getItemAtPosition(pos).toString().equals("1-17"))

but I was trying to do this inside a TextChangedListener and thought it would work but I find spinners really confusing.

Below is an example that is similar to what I have only I'm not at home so I tried to recreate it as much as possible. I left a number format exception that I have out of the code because I don't remember how i did it but my app was force closing if a number was entered and then deleted so 开发者_运维知识库i don't think its necessary for this.

EditText et1;
Spinner age;
RadioGroup radio;
RadioButton male;
RadioButton female;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calc);

    number = (EditText) findViewById(R.id.number);
    tv1 = (TextView) findViewById(R.id.tv1);
    male = (RadioButton) findViewById(R.id.male);
    female = (RadioButton) findViewById(R.id.female);
    radio = (RadioGroup) findViewById(R.id.radio);
    age = (Spinner) findViewById(R.id.ageselect);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.age_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        age.setAdapter(adapter);

textEdit.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable editable)
            {
             int number = Integer.parseInt(number.getText().toString());
             if (age.getItemAtPosition(0).toString().equals("1-17")){
             if (male.isChecked()){
             if (number == 40){
             tv1.setText("60"); }}
             if (female.isChecked()){
             if (number == 40){
             tv1.setText("50"); }}}

             if (age.getItemAtPosition(1).toString().equals("18-20")){
             if (male.isChecked()){
             if (number == 40){
             tv1.setText("65"); }}
             if (female.isChecked()){
             if (number == 40){
             tv1.setText("55"); }}}

            }

            @Override
            public void beforeTextChanged(CharSequence text, int start, int count, int after)
            {
                    // Do Stuff

            }

            @Override
            public void onTextChanged(CharSequence arg0, int start, int before, int count)
            {
                    // Do Stuff

            }

    });

I'd appreciate any recommendations or links where I can find this info. I've been searching around alot with no luck and this spinner is driving me crazy. I'm ready to just give it up and use more radio buttons but I dont want to clutter my app. Thanks guys!


Sorry for asking a repeat question. I used the following:

Object item = age.getSelectedItem();
if (item.equals("1-17")){
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜