Android - Working with Radio Groups, have seven radio buttons, just need value from 0-6
<p>I'm really new to Android and am trying to do something I think should be simple.</p>

<p>I have two activities, a MainActivity and an intent.  The intent is basically a set of 7 radio buttons and an OK and cancel button.</p>

<p>I'm trying to send a value (from 0-6 or 1-7) from MainActivity to set the checked radio button in the radio group and then return a value (from 0-6 or 1-7) back to the MainActivity depending on which option it selected in the radio group.  As far as I can tell getCheckedRadioButtonId() is not returning what I need.</p>

<p>Here's my code so far. </p>

<p>MainActivity:</p>

<pre><code>package com.android.phil.buttontest;
</code></pre>

<p>import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;</p>

<p>public class MainActivity extends Activity 
{
    protected static final int SUB_ACTIVITY_REQUEST_CODE = 1337;
    int maType = 1;</p>

<pre><code>@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button button = (Button) findViewById(R.id.setMA);
    button.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View arg0) 
        {
              Intent intent = new Intent(MainActivity.this, maTypeActivity.class);

              Bundle b = new Bundle();
              b.putInt("maType", maType);
              intent.putExtras(b);
              startActivityForResult(intent,SUB_ACTIVITY_REQUEST_CODE);
        }
    });

}

protected void onActivityResult(int requestCode, int resultCode,Intent data) 
{
        super.onActivityResult(requestCode, resultCode, data );

        // Here We identify the subActivity we started
        if(requestCode == SUB_ACTIVITY_REQUEST_CODE)
        {
            Bundle extras = data.getExtras();
            maType = extras.getInt("maType");
            Toast.makeText(this, "The data returned is: "+maType, Toast.LENGTH_SHORT).show();
        }
}
</code></pre>

<p>}</p>

<p>SubActivity</p>

<pre><code>package com.android.phil.buttontest;
</code></pre>

<p>import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;</p>

<p>public class maTypeActivity extends Activity 
{
    RadioGroup radioGroup1;
    Button ok;
    Button cancel;</p>

<pre><code>int maType = 0;
protected final int RESULT_OK = 1;

@Override
public void onCreate(Bundle icicle)
{
   super.onCreate(icicle);
   setContentView(R.layout.ma_type);

   Bundle b = this.getIntent().getExtras();
   maType = b.getInt("maType");
   radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
   radioGroup1.check(maType);

   Button ok = (Button) findViewById(R.id.okButton);
   Button cancel = (Button) findViewById(R.id.cancelButton);
   ok.setOnClickListener(new View.OnClickListener() 
   {
       public void onClick(View v) 
       {
           maType = radioGroup1.getCheckedRadioButtonId();
           Intent data = new Intent();                             
           data.putExtra("MAType",maType);                 
           maTypeActivity.this.setResult(RESULT_OK, data);
           maTypeActivity.this.finish();
       }
   });

   cancel.setOnClickListener(new View.OnClickListener() 
   {
       public void onClick(View v) 
       {
           maTypeActivity.this.finish();
       }
   });
}     
</code></pre>

<p>}</p>
