0

一旦选择了 3 个单选按钮之一,我就会尝试更新页面的视图,以便当我再次进入该视图时,我已经检查了该答案。我以滑动视图的形式正确获取所有数据,但是当我再次滑动回到同一个问题时,我选择的答案未显示。

我的活动.java

    MyQuizPagerAdapter adapter = new MyQuizPagerAdapter(questions,num_rows,quiz_subject,actionBar);
    ViewPager myPager = (ViewPager) findViewById(R.id.home_pannels_pager_quiz);
    myPager.setAdapter(adapter);

MyQuizAdapter.java

public class MyQuizPagerAdapter extends PagerAdapter {

String[][] questions;
int num_rows;
String quiz_subject;
ActionBar actionBar;
int position1;

public MyQuizPagerAdapter(String[][] questions, int num_rows, String quiz_subject, ActionBar actionBar)
{
    this.questions=questions;
    this.num_rows=num_rows;
    this.quiz_subject=quiz_subject;
    this.actionBar=actionBar;
}
// State number of pages
@Override
public int getCount() {
    return num_rows;
}

// Set each screen's content
@Override
public Object instantiateItem(View container, int position) {
    Context context = container.getContext();
    LinearLayout layout = new LinearLayout(context);
    actionBar.setSubtitle(questions[position][5]);
    position1=position;
    TextView textItem2 = new TextView(context);
    textItem2.setTextSize(20);
    textItem2.setTypeface(null, Typeface.BOLD);
    textItem2.setText("Question: "+(position+1));
    textItem2.setPadding(20, 0, 0, 0);

    TextView textItem = new TextView(context);
    //textItem.setGravity(Gravity.CENTER);
    textItem.setTextSize(13);
    textItem.setTypeface(null, Typeface.BOLD);
    textItem.setPadding(20, 20, 20, 40);


    textItem1.setTextSize(15);
    textItem1.setPadding(20, 50, 20, 20);*/

    RadioButton button1 = new RadioButton(context);
    RadioButton button2 = new RadioButton(context);
    RadioButton button3 = new RadioButton(context);
    button1.setTextSize(12);
    button2.setTextSize(12);
    button3.setTextSize(12);
    button1.setPadding(45, 5, 5, 7);
    button2.setPadding(45, 5, 5, 7);
    button3.setPadding(45, 5, 5, 7);
    button1.setId(R.id.Option1);
    button2.setId(R.id.Option2);
    button3.setId(R.id.Option3);

    RadioGroup rg = new RadioGroup(context);

    switch(Integer.parseInt(questions[position][7]))
    {
        case 0:
            break;
        case 1:
            rg.check(R.id.Option1);
            break;
        case 2:
            rg.check(R.id.Option2);
            break;
        case 3:
            rg.check(R.id.Option3);
            break;
        default:
            break;
    }

    layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    layout.setOrientation(LinearLayout.VERTICAL);

    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group,
                int checkedId) {

            switch (checkedId) { //set the Model to hold the answer the user picked
            case R.id.Option1:
                questions[position1][7]="1";
                actionBar.setSubtitle("option1");
                break;
            case R.id.Option2:
                questions[position1][7]="2";
                actionBar.setSubtitle("option2");
                break;
            case R.id.Option3:
                questions[position1][7]="3";
                actionBar.setSubtitle("option3");
                break;
            default:
                questions[position1][7]="0"; // Something was wrong set to the default
                actionBar.setSubtitle("error");
                break;
            }
        }
    });

textItem.setText(questions[position][1]);
button1.setText("A: "+questions[position][2]);
button2.setText("B: "+questions[position][3]);
button3.setText("C: "+questions[position][4]);
//textItem1.setText(content);
rg.addView(button1);
rg.addView(button2);
rg.addView(button3);
layout.addView(textItem2);
layout.addView(textItem);
//layout.addView(textItem1);
layout.addView(rg);
((ViewPager) container).addView(layout, 0); // This is the line I added
return layout;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}
}
4

1 回答 1

1

View Pager 回收(重新生成)视图(因此您没有得到选中哪个单选按钮的状态)

一种解决方案是再创建一个arraylist将保存状态并在instantiateItem()检查中将其arraylist更新为 View Pager 当前项目。

获得单击的按钮后,将该值存储到一个变量中。检查该instantiateItem()变量并更新单选按钮的 UI。

于 2013-05-15T11:05:06.250 回答