0

それぞれ1つの質問と最後のページに送信ボタンがある15ページのviewPagerを持つクイズアプリを作成しています.15の質問から選択されたオプションの最大数を取得したいと思います。以下は私のコードです

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);
    }
}

そして 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;
        }
    }
}

任意の助けをいただければ幸いです.Thanks

4

1 に答える 1

7

あなたはこのようにそれを得ることができます

int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);

// If you want position of Radiobutton
int position = group.indexOfChild(radioButton);

完全なコード

//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);
            }
        });
于 2015-02-05T13:43:26.527 に答える