我正在制作一个测验应用程序,其中有一个 viewPager,其中有 15 个页面,每个页面有 1 个问题,最后一页有一个提交按钮。现在我想在 15 个问题中选择最大选项数。下面是我的代码
问题PagerFragment.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;
}
}
}
任何帮助将不胜感激。谢谢