How to get id of selected radio button in a radio group
Asked Answered
A

1

0

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

Asis answered 5/2, 2015 at 13:41 Comment(0)
H
7

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);
            }
        });
Hirsute answered 5/2, 2015 at 13:43 Comment(3)
Thanks alot, I have one more doubt as I am flipping the screens flags are incrementing though I am not reselecting the radio buttonsAsis
No I want flag should be incremented only once means if a user visits the page again but doesnt change the answer then flag of selected option shouldnt be incremented as it is happening right nowAsis
that you need to check if user not change anything. flag will remain same. It should be in changing of fragment.Hirsute

© 2022 - 2024 — McMap. All rights reserved.