简体   繁体   中英

How to get RadioGroup value from different Activity

I am creating a Fitness Application with Android Studio. In this application, I have multiple Activities (6). In my SixthActivity, I want to be able to grab the selected radio button from the radio group in the ThirdActivity. However, when I do this, the value is empty (""), rather than the choice the user chose in the ThirdActivity.

I have tried using the experience Radio Button but it does not work either.

//Third Activity.java 

thirdRadioGroup = findViewById(R.id.thirdRadioGroup);

    int radioIdExperience = thirdRadioGroup.getCheckedRadioButtonId();
    //experience = (RadioButton) findViewById(radioIdExperience);


    Intent intent = new Intent(this, SixthActivity.class);
    intent.putExtra("Experience", radioIdExperience);
    //startActivity(intent);



//SixthActivity.java


    Intent intent=getIntent();
    String experience =intent.getStringExtra("Experience");



         sixthTextView.setText(experience);

I would like the textview in the SixthActivity to populate based on the choice the user made in the ThirdActivity radioGroup. For example, the three choices in the RadioGroup are :

0 -6 months 6 - 12 months 1 + year

Depending on whichever choice the user chooses in ThirdActivity I want it to display in the textview in the SixthActivity.

Problem :

According to your code . YOu are sending an Integer value to SixthActivity and Saving in String variable

ThirdActiivity

int radioIdExperience = thirdRadioGroup.getCheckedRadioButtonId();

SixthActivity

String experience =intent.getStringExtra("Experience");

Solutions : Make sure you are sending correct value and check this on THirdActivity first before sending . Then Check the sent value on SixthActivity

Hope this will help...

You can create a static public class an integer array as data member, and keep on populating the array after every subsequent startActivity(). And finally access that array in your sixth activity

Change:

String experience = intent.getStringExtra("Experience");

to:

String experience = String.valueOf(intent.getIntExtra("Experience"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM