0

I am trying to update the view of the page as soon as one of the 3 radio buttons are selected so that when i again come to that view i get that answer already checked. I am getting all data properly as swipe views but my selected answers is not shown when I swipe back to the same question again.

MyActivity.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;
}
}
Bhargav Vashi
  • 47
  • 1
  • 1
  • 8

1 Answers1

1

View Pager recycles(regenerates) the views (because of this you didn't get the state of which radio button is checked)

one solution is create one more arraylist which will hold the states and in the instantiateItem() check the arraylist and update the same to the View Pager current item.

after getting the button which is clicked , store that value into one variable. In instantiateItem() check that variable and update the UI of radio button.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96