0

Let's say I have a spinner(with 2 items) and a radiogroup (with 2 radiobuttons).

I want to show for example a TextView in my layout which will be with different values for every choice:

For example we have:

Spinner

Male
Female

RadioGroup

True False

So we have 4 cases:

  1. Male True
  2. Female False
  3. Male False
  4. Female True

In short, each time user selects any of these choices, I want to show a different textview. I have placed event listeners on spinners and radiobuttons:

Spinner Listener

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int arg2, long arg3) {
            ``// TODO Auto-generated method stub
        }

Radio Group Listener

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                          //switch checkedId and check which spinner item is selected...
                          //then show the textview
                     }

But this sometimes works sometimes not- I am having many and different problems. I also tried similar choices. But nothing worked preperly.

How can dynamically handle this without adding an extra button?

ElaGorilaki
  • 227
  • 6
  • 20

2 Answers2

1

Hopefully this better explains my comment.

int selectedSpinnerItem = -1;
int selectedRadioGroupItem = -1;

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        selectedSpinnerItem = position;
        updateViews();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        selectedSpinnerItem = -1;
        updateViews();
    }
});

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        selectedRadioGroupItem = checkedId;
        updateViews();
    }
});

public void updateViews(){
    // Here is where you will compare the values 
    // and display the appropriate views.
}
Larry McKenzie
  • 3,253
  • 25
  • 22
0

You can create all TextViews you want..

Than just change their visibilty..

TextView v = (TextView) findViewById(R.id.text1);
v.setVisibility(View.GONE); // Or view.VISIBLE, View.INVISIBLE

So you can visible the specific text view you want according to your logic. Or you can just create 2 textViews.. One will be for Male/Female and one for True/False;

I think the better thing to do is just change the text in on general textView.. But i am not sure what is the problem because i don't have code example that can help me understand

Hope this helps :)

Aviad
  • 1,539
  • 1
  • 9
  • 24
  • Thank you but this is not my problem. The problem is that I can not find a reliable way to know which spinner item was selected and which radio button at the same time. Not how will I change the textviews. – ElaGorilaki May 24 '14 at 21:44
  • Sorry but i am not sure i understand.. What the method spinner.getSelectedItem() and the same for radionButton. This link might help: http://www.mkyong.com/android/android-radio-buttons-example/ If you can explain and add you code parts i will be able to help more – Aviad May 24 '14 at 21:51