-1

Possible Duplicate:
How do I pass data between activities in Android?

qquestions

Yes,No,Difficult are three RadioButtons and NEXT is a Button. I don't know how to pass the id's of selected radio button of each screen to last screen. In RESULT screen particular value is to displayed in TextView. Here is a code for one screen:

public class MainActivity extends Activity {
RadioGroup rg;
RadioButton rb;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    addListenerOnButton();
}
private void addListenerOnButton() {
    rg = (RadioGroup) findViewById(R.id.rg_Ques1);
    b = (Button) findViewById(R.id.button1_ques1);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int  selectedId = rg.getCheckedRadioButtonId();
            rb = (RadioButton) findViewById(selectedId);
            Intent i = new Intent(MainActivity.this,Ques2.class);
            startActivity(i);
            Toast.makeText(getApplicationContext(), rb.getText(), 0).show();
        }
    });
}
Community
  • 1
  • 1
DroidLearner
  • 1,977
  • 5
  • 27
  • 46
  • since all the questions have same layout, you should rather use a single fragment and keep populating different questions and activity can keep track of correct answers – nandeesh Dec 17 '12 at 17:56

3 Answers3

0

In your initial class you can add an extra to the intent:

int  selectedId = rg.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(selectedId);
Intent i = new Intent(MainActivity.this,Ques2.class);
i.putExtra("rb_value", rb.isChecked());
startActvity(i);

In your second class you can get the value of the extra that you passed:

this.getIntent().getExtras().getBoolean("rb_value");

If you need the results from Question One for Question Two, this would be the best method. If you do not need the results from Question 1 for Question Two, but you will need them for Results only, it might be best to look into SharedPreference (From memory, some expressions may be incorrect but close):

In your questions activities:

SharedPreferences sharedPreferences = this.getSharedPreferences("my.app.preferences", 0);
SharedPreferences.Editor sharedPreferences_edit = sharedPreferences.edit();

sharedPreferences_edit.putBoolean("question1", true);
sharedPreferences_edit.commit();

In your Results activity:

SharedPreferences sharedPreferences = this.getSharedPreferences("my.app.preferences", 0);
sharedPreferences.getBoolean("question1");

For loading questions like this, loading up new intents is NOT the best solution, as you will end up layering your app... If i were creating something like this, I would make it able to change the question without having to load up a whole new activity, and upon button press, it will just change the question on the screen while storing the response to a variable/sharedPreferences.

Matt Clark
  • 24,947
  • 16
  • 62
  • 114
  • Can i get this value of extras in RESULT screen?? – DroidLearner Dec 17 '12 at 17:56
  • 1
    If you keep passing them from activity to activity sure. If you need the results of 1 in 2, use this, if the results of one are not needed until 5, you could use SharedPreferences instead of having to keep passing them. – Matt Clark Dec 17 '12 at 17:57
  • See the edit to my post for `SharedPreferences`. – Matt Clark Dec 17 '12 at 18:01
  • ya thanks.. kinda helpful! i'll inform you, once i succeed:) – DroidLearner Dec 17 '12 at 18:07
  • If you require assistance feel free to ask, but please show a genuine attempt ^.^ – Matt Clark Dec 17 '12 at 18:10
  • Can i do SharedPrefernces in onCreate() ? – DroidLearner Dec 17 '12 at 18:16
  • Yessir. Declare `SharedPreferences sharedPreferences` in your class, and then in the onCreate() function you set it valuse of `= this.getSharedPreferences...`. After doing that, you will be able to use SP anywhere in that class. – Matt Clark Dec 17 '12 at 18:18
  • `sharedPreferences.getBoolean("question1")` Here its taking two parameters, `key` and `defValue`. Whats that `defValue`? – DroidLearner Dec 17 '12 at 18:32
  • Default value, if it can not find any value set for that key, it will return what you say should be the default. – Matt Clark Dec 17 '12 at 18:33
  • so `defValue' true or false? – DroidLearner Dec 17 '12 at 18:35
  • Whatever you want it to be... If it tries to retrieve a value that you have not set, this is what will be returned instead of an error. – Matt Clark Dec 17 '12 at 18:42
  • I don't know how to do. can you please give me a solution/code? If anyone radio button is selected, it has to be show a Toast of selected item on RESULT page. here is my code http://pastebin.com/g5Z9EUHD – DroidLearner Dec 18 '12 at 01:01
  • If you are setting a boolean, your default value needs to be that of a boolean, so either tue or false. When you toast out the response, try using Toast.makeText(getApplicationContext(), "Q1 = " + Boolean.toString(s); – Matt Clark Dec 18 '12 at 01:28
  • Instead of `boolean`, i have used `String` in MainActivity `ed.putString("q1", s);` and in Result page `String s1 = preferences1.getString("q1", "");` And I'm getting those selected values..Which is better? `String` or `boolean`? – DroidLearner Dec 18 '12 at 01:38
  • do some research for yourself on google and Android Developers... In the first activity you will need to say the user clicked yes or no, true or false, so you could just use boolean. `.setBoolean("q1", true);` in the second activity you need to retrieve the value of q1 using whatever you used to store it. `.getBoolean("q1", defValue);` If you need to use more complex values, go ahead and use String, it does not matter to me.. do whatever fits your app. – Matt Clark Dec 18 '12 at 01:52
0

You can use intents or you can store the values in preferences and access preferences wherever you need.

Durairaj Packirisamy
  • 4,297
  • 1
  • 19
  • 26
0

My approach will be different here. I will make only one activity for all the questions. I will keep my questions in a list and load new questions on 'next' button press. When last question is displayed change the button text to 'finish' and load new activity on pressing 'finish' with the correct answers' number in a bundle.

Vasudev
  • 1,318
  • 12
  • 14