2

I'm Developing an android app in which the Questionnaire activity contains Questions which as radio Buttons and also a Button.So when the button is pressed I've to check whether all the Questions are answered.if the Question is not answered then a alert message should pop up stating that the particular Question no is not answered.Can anyone please help me with the java code. Thanks in Advance.

This is my Questionnaire with radio buttons and a button placed below

<RadioGroup
    android:id="@+id/Mquestion1"  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb1" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb2" />

<RadioButton
    android:id="@+id/radioButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb3" />
</RadioGroup>
<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_view"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb1" />

<RadioButton
    android:id="@+id/radioButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb2" />

<RadioButton
    android:id="@+id/radioButton6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb3" />
</RadioGroup>
<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_view"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb1" />

<RadioButton
    android:id="@+id/radioButton8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb2" />

<RadioButton
    android:id="@+id/radioButton9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="44dp" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="sendMessage"
        android:text="@string/MQ1_next" />

</RelativeLayout>

</LinearLayout>

and here is the java code

public class ManagerQuestionnaire1 extends Activity
{

Button next;
RadioGroup rg1;
RadioGroup rg2;
RadioGroup rg3;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    final RadioGroup    rg1=(RadioGroup)findViewById(R.id.Mquestion1);
     final RadioGroup   rg2=(RadioGroup)findViewById(R.id.Mquestion2);
     final RadioGroup   rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    Button next=(Button)findViewById(R.id.button1);
            next.setOnClickListener(new View.OnClickListener()
            {
                public void OnClick(View v) //Here in this line I'm getting a caution symbol which says REMOVE METHOD 'OnClick'
                {

                    if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || rg1.getCheckedRadioButtonId()!=R.id.radioButton2 || rg1.getCheckedRadioButtonId()!=R.id.radioButton3)||(rg2.getCheckedRadioButtonId()!=R.id.radioButton4 || rg2.getCheckedRadioButtonId()!=R.id.radioButton5 || rg2.getCheckedRadioButtonId()!=R.id.radioButton6)||(rg3.getCheckedRadioButtonId()!=R.id.radioButton7 || rg3.getCheckedRadioButtonId()!=R.id.radioButton8 || rg3.getCheckedRadioButtonId()!=R.id.radioButton9))
                    {
                         AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
                         alert.setTitle("Exception:Complete the Questions");
                         alert.setMessage("Please ensure all Questions are answered");
                    }
                    else    
                    {
                         Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
                            startActivity(intent);
                    }
                }

                @Override
                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub

                }


    });
}
Steve
  • 117
  • 2
  • 3
  • 11
  • Just something that you might face in the future. If you plan on using `RadioGroup` and its [`listener`](http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html), keep in mind that it raises the event multiple(3, to be exact) times. [Here](http://stackoverflow.com/questions/10263778/radiogroup-calls-oncheckchanged-three-times) is some confirmation. – Yordan Lyubenov Mar 06 '14 at 15:25

2 Answers2

1

You should check on the SelectedID of the radioGroup and check for the return ID and their corresponding ids as following:

0)Declare all the used UI component in the global scoop of your code as following:

Class x extends Activity{
    RadioButton radioButton1;
    ...

1)Initialize all the XML buttons to get each button ID in OnCreate as following

radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
//The rest of other buttons
....
....

2)In the clickListener

next.setOnClickListener(new View.OnClickListener()
{
 public void OnClick(View v)
{
 if(Mquestion1.value!=radioButton1 || Mquestion1.value!=radioButton2 || Mquestion1.value!=radioButton3 || Mquestion1.value!=radioButton4) ||(Mquestion2.valu!= radioButton5 || ..... )||(Mquestion3.value....)
{
      //Show the allert
}
}
});

Replace the dots with your rest checks.

Use the following code instead of yours

Button next;
    RadioGroup rg1;
    RadioGroup rg2;
    RadioGroup rg3;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    rg1=(RadioGroup)findViewById(R.id.Mquestion1);
    rg2=(RadioGroup)findViewById(R.id.Mquestion2);
    rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    next=(Button)findViewById(R.id.button1);
    next.setOnClickListener(new View.OnClickListener()
    {
        public void OnClick(View v)
        {
            if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton2 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton3)||(Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton4 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton5 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton6)||(Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton7 || Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton8 || Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton9))
            {
                 AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
                 alert.setTitle("Exception:Complete the Questions");
                 alert.setMessage("Please ensure all Questions are answered");
            }
            else    
            {
                 Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
                    startActivity(intent);
            }
        }
    });
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • @Steve check the modified answer – Sami Eltamawy Mar 06 '14 at 15:53
  • 1
    @Steve check the modified answer – Sami Eltamawy Mar 06 '14 at 16:54
  • put the cursor of the mouse on the begining of the error line and click (CTRL+1) and select import (if it exist) and select add unimplemented method – Sami Eltamawy Mar 06 '14 at 18:33
  • Please if you find my info useful vote my answer up or accept it as the answer – Sami Eltamawy Mar 06 '14 at 18:33
  • Abd, I've done all that. I've got one error left which stops my app running.the error is in the line-->public void OnClick(View v) \\when i click the caution symbol I get a message that REMOVE method 'OnClick' cause of this I'm stuck! What I've to do ? – Steve Mar 06 '14 at 18:46
  • 1
    write `next.setOnClickListener` and press enter. Then put the cursor inside the parses and write `new ` then (CTRL+Space) and select `onClick...` – Sami Eltamawy Mar 06 '14 at 18:49
  • Abd, Check my post I've updated the java code with what u said and also I've mentioned what&where is the problem using comment line.Please check and tell me a solution. btwn Thanks for your time and concern! – Steve Mar 06 '14 at 19:12
  • I will, but please appreciate my efforts with a vote up on my answer – Sami Eltamawy Mar 06 '14 at 19:14
0

If you generate your questions, so you don't have any IDs for your views, you can cycle through all RadioGroups and through all their children in RadioGroup. So if you store RadioGroups for example in an ListArray radioGroups, then you can do this:

private boolean isAllAnswered() {
    for (RadioGroup rg : radioGroups) {
        boolean questionAnswered = false;
        for (int i = 0; i < rg.getChildCount(); i++) { 
            RadioButton rb = rg1.getChildAt(i);
            if (rb.isChecked()) {
                questionAnswered = true;
                break; // litle optimalization
            }
        }
        if (questionAnswered == false) {
            return false;
        }
    }
    return true;
}

If you need to know, which questions are not answered, you can expand it to returning an Array.

If you use xml, then make an common method isAnswered(RadioGroup rg) and skip the first cycle. It's up to you.

nonahex
  • 677
  • 1
  • 6
  • 9