2

I have so far the following code for that allows me to set a value to a textview when I click between toggle buttons that are grouped in a radiogroup. For some reason, it seems like the OnCheckedChanged does not get called. Is this the appproach for this?

//onClickListener method that returns an interface
private View.OnClickListener createClickListener(final int value) {
    return new View.OnClickListener()  {

        @Override
        public void onClick(View view) {

            buttonValue = value;
            buttonState = true;

            ToggleButton clickedButton = (ToggleButton) view;
            RadioGroup radioGroup= (RadioGroup) clickedButton.getParent();
            final TextView num = (TextView) view.findViewById(R.id.num);

            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch(checkedId){
                            case R.id.number_zero:
                                buttonValue = value;
                                num.setText(buttonValue);
                                System.out.println("Button Value" + buttonValue);
                            case R.id.number_one:
                                buttonValue = value;
                                num.setText(buttonValue);
                                System.out.println("Button Value" + buttonValue);
                            case R.id.number_two:
                                buttonValue = value;
                                num.setText(buttonValue);
                                System.out.println("Button Value" + buttonValue);
                            case R.id.number_three:
                                buttonValue = value;
                                num.setText(buttonValue);
                                System.out.println("Button Value" + buttonValue);
                    }
                    System.out.println("Check changed listener called");
                }
            });

            for (int i = 0; i < radioGroup.getChildCount(); ++i) {
                View nextChild = radioGroup.getChildAt(i);
                if (!(nextChild instanceof ToggleButton)) {
                    continue;
                }
                if (nextChild.getId() != clickedButton.getId() || !clickedButton.isChecked()) {
                    ToggleButton tb2 = (ToggleButton) nextChild;
                    tb2.setChecked(false);
                }
            }

        }

    };
}

Calling using:

zero.setOnClickListener(createClickListener(0));
one.setOnClickListener(createClickListener(1));
two.setOnClickListener(createClickListener(2));
three.setOnClickListener(createClickListener(3))

EDIT:

For some reason, the text is not being set to the TextView of num.

I changed above from

final TextView num = (TextView) view.findViewById(R.id.num);
num.setText(value);

to

num = (TextView) findViewById(R.id.num);
num.setText(Integer.toString(value));

making this:

  TextView num; 

a global variable.

I used the debugger and num is not null and the value is number of the button.

Kala J
  • 2,040
  • 4
  • 45
  • 85
  • This is quite a confusing approach in my opinion. You're only setting the `OnCheckedChangedListener` on the radio group after the radio button has been clicked. That's probably too late. I'm not sure of the order in which the various events are processed, but it seems to me that once you've processed the click, the "checked changed" event may already be lost. – Dawood ibn Kareem Nov 04 '14 at 18:46
  • oh I see, what would you propose would be better? – Kala J Nov 04 '14 at 18:56
  • I would call the `radioGroup.setOnCheckedChangeListener( ... )` stuff separately, outside of the `OnClickListener` code. – Dawood ibn Kareem Nov 04 '14 at 18:57
  • what if I wanted both actions to happen onClick though? – Kala J Nov 04 '14 at 19:10
  • If you want both actions to happen onClick, then you don't need an onCheckedChanged listener at all. But you could put the code that processes the change into either type of listener. You could test it both ways and see which works better for your application, but I'd be surprised if you saw too much of a difference. – Dawood ibn Kareem Nov 04 '14 at 19:19
  • Thanks I got it checking but I'm now getting a NPE even though value is a number – Kala J Nov 04 '14 at 19:43
  • OK, what line is the NPE on? – Dawood ibn Kareem Nov 04 '14 at 20:00
  • So, I'm confused. Are you still getting the NPE? If so, I still don't know which line it's on. – Dawood ibn Kareem Nov 04 '14 at 20:49
  • nope, I'm sorry. I fixed it. NPE no longer :( Now, it's just acting strange... in the sense that I'm not sure why when I set the text for the number, the text does not change. – Kala J Nov 04 '14 at 20:51
  • OK, so if I understand you correctly, you're basically calling `setText` on a `TextView` and it's not changing to the value you pass in. Just as an experiment - what happens if you change the value that you passed to a hardcoded constant? (BTW, +1 for using the debugger) – Dawood ibn Kareem Nov 04 '14 at 20:59
  • Still not being set. Weird :O – Kala J Nov 04 '14 at 21:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64269/discussion-between-kala-j-and-david-wallace). – Kala J Nov 04 '14 at 21:10
  • I'm clutching at straws a bit here, but are you sure that `num` references the `TextView` that you think it does? Is that something you can check with the debugger? – Dawood ibn Kareem Nov 04 '14 at 21:21
  • I just noticed, this strange bug only happens my first view. In my other views, it works fine. It's interesting. – Kala J Nov 05 '14 at 00:52
  • Yes, that's what I'd expect. I hope my first comment above explains why this is happening. – Dawood ibn Kareem Nov 05 '14 at 01:52
  • Maybe. I also think it could be related to this: http://stackoverflow.com/questions/11794269/onpageselected-isnt-triggered-when-calling-setcurrentitem0 – Kala J Nov 05 '14 at 03:34
  • Probably not. That looks like a completely different set of events. – Dawood ibn Kareem Nov 05 '14 at 04:31
  • why? It seems like it could be because it is only happening on the first view in my viewpager. The other views are working perfectly and the id is definitely correct. – Kala J Nov 05 '14 at 15:20
  • @DavidWallace, here's a video on what is happening. https://www.dropbox.com/s/cfx2r3t3umhgc4p/VIDEO0048.mp4?dl=0 – Kala J Nov 05 '14 at 18:18

0 Answers0