I am making a quiz app in which I have a viewPager which has 15 pages in it having 1 question each and a submit button at the last page.Now I want to get the Maximum number of option selected among 15 questions. Below is my code
QuestionPagerFragment.java:
public class QuestionPagerFragment extends Fragment {
protected View mView;
String pageData[]; //Stores the text to swipe.
String optionData[];//Stores the option data.
int rid;
RadioGroup group;
String ans;
String ans1;
String ans2;
SharedPreferences prefs;
public static final String MyPREFERENCES = "MyPrefs" ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.page, container, false);
return mView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
prefs = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
pageData=getResources().getStringArray(R.array.desserts);
optionData=getResources().getStringArray(R.array.options);
group = (RadioGroup)mView.findViewById(R.id.group);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
checkedId=group.getCheckedRadioButtonId();
rid=checkedId;
save();
}
});
((TextView)mView.findViewById(R.id.textMessage)).setText(pageData[getArguments().getInt("pos")]);
if(getArguments().getInt("pos") == 14) {
((Button)mView.findViewById(R.id.submitbtn)).setVisibility(View.VISIBLE);
((Button)mView.findViewById(R.id.submitbtn)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
System.out.println("AAAAAAAAAAAAAAAAA-------------"+MainActivity.FLAG_A);
System.out.println("BBBBBBBBBBBBBBBBB-------------"+MainActivity.FLAG_B);
System.out.println("CCCCCCCCCCCCCCCCC-------------"+MainActivity.FLAG_C);
System.out.println("DDDDDDDDDDDDDDDDD-------------"+MainActivity.FLAG_D);
click();
}
});
}
for(int i = 0; i < group.getChildCount(); i++){
((RadioButton) group.getChildAt(i)).setText(optionData[(getArguments().getInt("pos")*4)+i]);
}
}
public void save() {
if(rid==2131034179){
MainActivity.FLAG_A++;
}
else if(rid==2131034180){
MainActivity.FLAG_B++;
}
else if(rid==2131034181){
MainActivity.FLAG_C++;
}
else if(rid==2131034182){
MainActivity.FLAG_D++;
}
}
public void click(){
ans = String.valueOf(Math.max(MainActivity.FLAG_A, MainActivity.FLAG_B));
ans1 = String.valueOf(Math.max(Integer.parseInt(ans), MainActivity.FLAG_C));
ans2 = String.valueOf(Math.max(Integer.parseInt(ans1), MainActivity.FLAG_D));
String a = ans2;
SharedPreferences prefss = getActivity().getSharedPreferences( "idValue", 0 );
SharedPreferences.Editor editor = prefss.edit();
editor.putString( "idValue", a);
editor.commit();
Log.e("Shared Pref","-----------------------" +a);
}
}
And MainActivity.java:
public class MainActivity extends FragmentActivity {
String pageData[]; //Stores the text to swipe.
String optionData[];//Stores the option data.
LayoutInflater inflater; //Used to create individual pages
ViewPager vp; //Reference to class to swipe views
Button submit;
public static int FLAG_A;
public static int FLAG_B;
public static int FLAG_C;
public static int FLAG_D;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pageData=getResources().getStringArray(R.array.desserts);
optionData=getResources().getStringArray(R.array.options);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vp=(ViewPager)findViewById(R.id.viewPager);
vp.setAdapter(new ScreenSlidePagerAdapter(getSupportFragmentManager()));
}
private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment f = new QuestionPagerFragment();
Bundle b = new Bundle();
b.putString("que", pageData[position]);
b.putInt("pos", position);
f.setArguments(b);
return f;
}
@Override
public int getCount() {
return pageData.length;
}
}
}
Any help would be appreciated.Thanks
Shabbir Dhangot
you can get it like this
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
// If you want position of Radiobutton
int position = group.indexOfChild(radioButton);
Complete code
//complete code..
//rgp is your radio group
rgp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
int position = group.indexOfChild(radioButton);
}
});
来源:https://stackoverflow.com/questions/28345451/how-to-get-id-of-selected-radio-button-in-a-radio-group