views:

100

answers:

3

Ok, I'm not great at explaining this as I'm new to Android so work with me. I have tried everything I can find and I dont know how to make everything work together. I have a Spinner to select an age group, a Radio group with Radio buttons to allow you to select Gender (male or female), I have an EditText that I will allow for numerical input and a Textview out to the side that is currently blank.

Depending on which age group is selected from the spinner and which one of the Radio buttons are selected - setText of the TextView to a certain number based on what number is entered in the EditText box.

Ive tried using OnItemSelectedListener for the spinner but havent been able to find how it knows what is selected from the array.I tried getItemSelected with an if statement with the item from the array

for the radio button Ive tried using an if statement - if (radiobutton.isChecked()) but I didnt know if I needed an ifCheckedRadio before this and how to make it work after the spinner

and if all of this worked I had if (EditText.getText().equals("#")) TextView.setText("#");

This will be copied for each possible # entered

now would the text change like this without a button and OnClickListener or is there a way to do this without a button becuase there will more than one EditText that will be added at the end using a button but I wanted this displayed immediately after it is entered

Sorry if this isnt clear, please let me know if you need more information. If I've overlooked a previous question that uses spinners and radiobuttons please let me know and I'll keep looking but I've been stuck on this.

A: 

This should do it

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

          TextView tv = (TextView) findViewById(R.id.TextView01);
          RadioGroup rg = (RadioGroup) findViewById(R.id.RadioGroup01);
          EditText et = (EditText) findViewById(R.id.EditText01);
          RadioButton rb1 = (RadioButton) findViewById(R.id.RadioButton01);
          RadioButton rb2 = (RadioButton) findViewById(R.id.RadioButton02);
          String gender = "";
          if (rb1.isChecked())
             gender = "male";
          if (rb2.isChecked())
             gender = "female";
          String answer;
          answer = parent.getItemAtPosition(pos).toString() + " "
          + gender + " " + et.getText(); 
          tv.setText(answer);
    }
  • rough and ready but should give you the general idea (put the Radiobuttons inside the RadioGroup by the way)
NickT
I was trying to go a different way with it but this might make things alot easier. The only thing I dont understand and that im still trying to figure out is that everytime I find something about a spinner and getitem i dont understand where the actual item being selected goes. answer = parent.getItemAtPosition(pos).toString() + " " + gender + " " + et.getText();
Daniel
I was trying to edit this to make sense but ran out of time. What is " " after toString and after gender?
Daniel
The method tells you which item was clicked/selected, it's at position 'pos'. I doesn't 'go' anywhere until you access it with getItemAtPosition and turn it into a String. The 'answer' is a concatenation of Stings. The " " is just a string with a space in it to separate the words.
NickT
I had the RadioButtons in the RadioGroup but I was putting my buttons and text under OnCreate instead of under OnItemSelected so thank you, that alone has helped me. I cannot use the answer string like that because there will be multiple possibilities for what text is input and I dont need it to display everything together just display a certain number if a number is entered in the EditText while a certain RadioButton is selected and a certain age group which I believe will have to be accomplished using if statements but maybe I can work it into a string.
Daniel
This has helped and pointed me in the right direction. To clarify though 'pos' will be the position of the spinner that is selected and will be a numerical value as the items in the spinner would be numbered from top to bottom? I think using strings will make this easier.
Daniel
A: 

I added addOnItemSelectedListener but I dont know if I need it and if I do do I have to implement it in the activity? This does not work and I dont know that it can but this is what I want to happen -

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



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

        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);

    age.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> parent, 
                  View view, int pos, long id) {

        et1 = (EditText) findViewById(R.id.et1);
        tv1 = (TextView) findViewById(R.id.tv1);
        male = (RadioButton) findViewById(R.id.male);
        female = (RadioButton) findViewById(R.id.female);
        radio = (RadioGroup) findViewById(R.id.radio);

                       String input = et1.getText().toString();
        if (parent.getItemAtPosition(pos).toString().equals("1-17"))
             if (male.isChecked())
        if ("42".equals(input))
             {

        tv1.setText("60"); }
        }           
        }

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

Would male in the if statement have to be "male"? Would 1-17 have to be "1-17"? 1-17 is the first item in the array. I tried every variation of this I can think of and i think I just dont understand it. Hopefully this gives you the idea of what I'm trying to do. Depending on what gender and age group are selected the text view will be set to a certain number based on what number is entered into the edittext box.I realize that not everthing is used but some of it will be later.Thanks guys.

Daniel
The statement if (gender.equals(male));is wrong. That means just the semicolon itself( a NOP) gets executed. Don't follow if statements with ';'s!
NickT
Ok, so would I be able to use -if (gender.equals(male))if (spin.equals(1-17))if (et1.equals("40")){ pushout.setText("60"); }Or could I combine all three into one if statement somehow?
Daniel
I'm sorry tv1 is actually pushout so I realize that this is wrong but I just changed everything back to a generic before posting and left that the same.
Daniel
I edited this and it works now as long as I select from my spinner after I select the male radiobutton and enter "42". Any ideas on how to get it to work without having to select from the spinner after making the other selections
Daniel
A: 

OK - re your last comment, this makes both selecting an item from the spinner and the radio buttons, terminating actions for input. Basically I've added onClickListeners to the buttons. You can't really add an Onclick to the edit text though, as a click normally brings up the soft keyboard.

package com.test.spinnerexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerExample extends Activity {
RadioButton rb1; RadioButton rb2; TextView tv;  EditText et;
Spinner sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    rb1 = (RadioButton) findViewById(R.id.RadioButton01);
    rb2 = (RadioButton) findViewById(R.id.RadioButton02);
    tv = (TextView) findViewById(R.id.OutputText);
    et = (EditText) findViewById(R.id.EditText01);
    sp = (Spinner) findViewById(R.id.Spinner01);
    ArrayAdapter<CharSequence> adapter1;
    adapter1 = ArrayAdapter.createFromResource(
            this, R.array.agegroups_array,
            android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(adapter1);
    rb1.setChecked(true);
    sp.setOnItemSelectedListener(new MyOnItemSelectedListener());
    rb1.setOnClickListener(new ButtonListener());
    rb2.setOnClickListener(new ButtonListener());
}
private class MyOnItemSelectedListener implements OnItemSelectedListener {

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

        RadioButton checkedButton = rb1.isChecked() ? rb1 : rb2;
        showResult(parent.getItemAtPosition(pos).toString(),
                checkedButton.getText().toString());
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }
}
private class ButtonListener implements OnClickListener{

    @Override
    public void onClick(View view) {
        RadioButton rb = (RadioButton) view;
        int selectedItem = sp.getSelectedItemPosition();
        showResult(sp.getItemAtPosition(selectedItem).toString(), 
                rb.getText().toString());
    }

}
private void showResult(String selctdSpnrText, String chkdBtnText){
    String answer = selctdSpnrText + " " + chkdBtnText + " "
    + et.getText();
    tv.setText(answer);
}
}
NickT
This wasnt exactly what I meant but you definitely helped me out alot and what I ended up with was adding - et.addTextChangedListener(new TextWatcher() so that if a certain number is entered in the EditText the TextView will immediately change.I will post the code later on. Thanks for all your help.
Daniel