Radiobutton get not been unchecked!

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Radiobutton get not been unchecked!

Postby marlan » Tue Mar 03, 2009 8:07 am

Hi,
when I check a radiobutton, the radiobutton cannot been unchecked (the button stays checked even if you check another one), code looks like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private void addRadioGroup(ViewGroup view,final Question question){
  2.  
  3.                 RadioGroup group = new RadioGroup(this);
  4.  
  5.        
  6.  
  7.                 group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
  8.  
  9.                 {
  10.  
  11.                     public void onCheckedChanged(RadioGroup group, int checkedId)
  12.  
  13.                     {
  14.  
  15.                         for(Answer answer: question.shuffledAnswers){
  16.  
  17.                                 if(checkedId == answer.id){
  18.  
  19.                                         answer.state = true;
  20.  
  21.                                 }else{
  22.  
  23.                                         answer.state = false;
  24.  
  25.                                 }
  26.  
  27.                         }
  28.  
  29.                     }
  30.  
  31.                 });
  32.  
  33.                 for(Answer answer: question.shuffledAnswers){
  34.  
  35.                         RadioButton r = new RadioButton(this);
  36.  
  37.                        
  38.  
  39.                         r.setText( answer.answer);
  40.  
  41.                         r.setId(answer.id);
  42.  
  43.                        
  44.  
  45.                         //cannot been unchecked, also tried group.check(r.getId())???
  46.  
  47.                         if(answer.state){
  48.  
  49.                                 r.setChecked(answer.state);
  50.  
  51.                         }
  52.  
  53.                         group.addView(r);
  54.  
  55.                 }
  56.  
  57.                 view.addView(group);
  58.  
  59.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


It is described in the issue:
1772
but the issue is closed (I don't understand the answer)!?
Can someone point me in the right direction?
Thanks Mark
marlan
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Jan 22, 2009 10:02 pm

Top

Postby marlan » Fri Mar 06, 2009 8:30 am

I have fixed my problem.
Now it works as expected:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private void addRadioGroup(ViewGroup view,final Question question){
  2.  
  3.                 RadioGroup group = new RadioGroup(this);
  4.  
  5.  
  6.  
  7.                
  8.  
  9.                 group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
  10.  
  11.                 {
  12.  
  13.                     public void onCheckedChanged(RadioGroup group, int checkedId)
  14.  
  15.                     {
  16.  
  17.                         for(Answer answer: question.shuffledAnswers){
  18.  
  19.                                 if(checkedId == (answer.id)){
  20.  
  21.                                         answer.state = true;
  22.  
  23.                                 }else{
  24.  
  25.                                         answer.state = false;
  26.  
  27.                                 }
  28.  
  29.                         }
  30.  
  31.                     }
  32.  
  33.                 });
  34.  
  35.  
  36.  
  37.                 int checked = 0;
  38.  
  39.                 for(Answer answer: question.shuffledAnswers){
  40.  
  41.                         RadioButton r = new RadioButton(this);
  42.  
  43.                         r.setText(answer.answer);
  44.  
  45.                         r.setId(answer.id);
  46.  
  47.                         if(answer.state){
  48.  
  49.                                 checked = answer.id;
  50.  
  51.                         }
  52.  
  53.                        
  54.  
  55.                         group.addView(r);
  56.  
  57.                 }
  58.  
  59.                 if(     checked >       0       ){
  60.  
  61.                         group.clearCheck();
  62.  
  63.                         group.check(checked);
  64.  
  65.                 }
  66.  
  67.                 view.addView(group);
  68.  
  69.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
marlan
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Jan 22, 2009 10:02 pm

Postby zehunter » Sun Jan 10, 2010 11:20 pm

marlan wrote:I have fixed my problem.
Now it works as expected:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private void addRadioGroup(ViewGroup view,final Question question){
  2.                 RadioGroup group = new RadioGroup(this);
  3.  
  4.                
  5.                 group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
  6.                 {
  7.                     public void onCheckedChanged(RadioGroup group, int checkedId)
  8.                     {
  9.                         for(Answer answer: question.shuffledAnswers){
  10.                                 if(checkedId == (answer.id)){
  11.                                         answer.state = true;
  12.                                 }else{
  13.                                         answer.state = false;
  14.                                 }
  15.                         }
  16.                     }
  17.                 });
  18.  
  19.                 int checked = 0;
  20.                 for(Answer answer: question.shuffledAnswers){
  21.                         RadioButton r = new RadioButton(this);
  22.                         r.setText(answer.answer);
  23.                         r.setId(answer.id);
  24.                         if(answer.state){
  25.                                 checked = answer.id;
  26.                         }
  27.                        
  28.                         group.addView(r);
  29.                 }
  30.                 if(     checked >       0       ){
  31.                         group.clearCheck();
  32.                         group.check(checked);
  33.                 }
  34.                 view.addView(group);
  35.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


i can you explain?
i not really understand your solution.

on my side, i try to dynamicly create radio button, and check this last one.

i do :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. radiogroup.clearcheck();
  3.  
  4.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

then
create radiobutton insiode radiogroup

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         RadioButton newRadioButton = new RadioButton(this);
  3.  
  4.         newRadioButton.setText(label);
  5.  
  6.         newRadioButton.setChecked(true);
  7.  
  8.  
  9.  
  10.         radiogroup.addView(newRadioButton, layoutParams);
  11.  
  12.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


but lastest radio button is check, but oneanother too... and he can not be unckecd, like he is no part of the radiogroup ...

any idea ?
zehunter
Developer
Developer
 
Posts: 27
Joined: Sun Jan 10, 2010 10:45 am

Postby marlan » Mon Jan 11, 2010 8:23 am

I think, my fault was to call setCkecked on the radiobutton, instead I have to call check with the radiobutton id on the radiogroup (and the missed call on clearcheck).
marlan
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Jan 22, 2009 10:02 pm

Postby zehunter » Mon Jan 11, 2010 9:11 am

marlan wrote:I think, my fault was to call setCkecked on the radiobutton, instead I have to call check with the radiobutton id on the radiogroup (and the missed call on clearcheck).


oh, good idea that i need to try ... well i will need to find how to get ID of a new dynamic radioButton :-)

thanks for your help! :-)
zehunter
Developer
Developer
 
Posts: 27
Joined: Sun Jan 10, 2010 10:45 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests