2013-04-13 69 views
0

我試圖在3個單選按鈕中的一個被選中時立即更新頁面的視圖,以便當我再次訪問該視圖時,我已經查看了該答案。 我正在以滑動視圖正確地獲取所有數據,但是當我再次滑回相同問題時,我的所選答案未顯示。如何更新PagerAdapter中的選定答案單選按鈕?

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

回答

1

查看傳呼機回收(再生),因爲這樣的意見(你沒有得到其國家廣電按鈕被檢查)

一個解決方案是創建一個更多arraylist將保存狀態並在instantiateItem()中檢查arraylist並將其更新爲View Pager當前項目。

獲取被點擊的按鈕後,將該值存儲到一個變量中。 在instantiateItem()檢查該變量並更新單選按鈕的UI。

相關問題