2013-07-26 65 views
0
ArrayList <HashMap<String,String>> quesion_list,answerkey; 

    viewPager = (ViewPager) findViewById(R.id.pager); 
      adapter = new ViewPagerAdapter(PracticeModeActivity.this,quesion_list,A1,A2,A3,A4,A5); 
        // Binds the Adapter to the ViewPager 
       viewPager.setAdapter(adapter); 
       viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
        @Override 
        public void onPageScrolled(int i, float v, int i2) { 

        } 

        @Override 
        public void onPageSelected(int i) { 

        // Log.i("page",String.valueOf(i)); 
         viewPager.getAdapter().getCount(); 
         int current = viewPager.getCurrentItem(); 
         int current_count=current+1; 
        // Log.i("count",String.valueOf(viewPager.getAdapter().getCount())); 
         noquestions.setText(String.valueOf(current_count)+" of "+length); 

        // Log.i("options",options.toString()); 
//     if(i!=0) 
//     { 
//      answerA1.setChecked(false); 
//      answerA2.setChecked(false); 
//      answerA3.setChecked(false); 
//      answerA4.setChecked(false); 
// 
//     } 

        } 

        @Override 
        public void onPageScrollStateChanged(int i) { 
       //  viewPager.getParent().requestDisallowInterceptTouchEvent(true); 
        } 
       }); 

    public class ViewPagerAdapter extends PagerAdapter { 
      // Declare Variables 
      Context context; 
      String[] rank; 
      String[] country; 
      String[] population; 
      int[] flag; 
      LayoutInflater inflater; 
      private ArrayList<HashMap<String, String>> hashmap; 

      public ViewPagerAdapter(Context context,ArrayList<HashMap<String, String>> hashMaps) { 
       this.context = context; 
       this.hashmap = hashMaps;    

      } 

      @Override 
      public int getCount() { 
       return hashmap.size(); 
      } 

      @Override 
      public boolean isViewFromObject(View view, Object object) { 
       return view == ((RelativeLayout) object); 
      } 
      @Override 
      public void finishUpdate(View container) { 
      } 

      @Override 
      public void restoreState(Parcelable state, ClassLoader loader) { 
      } 

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

      @Override 
      public void startUpdate(View container) { 
      } 
      @Override 
      public Object instantiateItem(final ViewGroup container, final int position) { 

       // Declare Variables 
       TextView txtrank; 

       inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       itemView = inflater.inflate(R.layout.question_item, container,false); 
       TextView correctanswer=(TextView)itemView.findViewById(R.id.correctanswer); 
    ((ViewPager) container).addView(itemView); 

       r1=(RadioButton)itemView.findViewById(R.id.answerA1); 
       return itemView; 
      } 
    } 
/* button onclick method */ 
public void Clear(View v) 
{ 

      viewPager.findViewWithTag(viewPager.getCurrentItem()); 
      int current = viewPager.getCurrentItem(); 
      Log.i("click",String.valueOf(current)); 
    //  View view=viewPager.getChildAt(current); 
    //  ((ViewGroup))mViewPager.getChildAt(MyActivity.mViewPager.getCurrentItem())); 
      View view  = viewPager.getChildAt(viewPager.getCurrentItem()); 
      options.set(current, "0"); 
    //  adapter = new ViewPagerAdapter(PracticeModeActivity.this,quesion_list); 
      // Binds the Adapter to the ViewPager 
    // viewPager.setAdapter(adapter); 
     viewPager.getAdapter().notifyDataSetChanged(); 
      RadioButton rl11=(RadioButton)v.findViewById(R.id.answerA1); 
    //   
      rl11.setChecked(false); 
    //  

     } 

在這裏,我使用viewpager adaper爲listview.Clear是一個onclick方法外的adapter.if我點擊按鈕,單選按鈕被禁用的第一個position.after,我得到空指針異常。是否有任何想法來糾正這個問題?我需要在這方面努力。任何人都可以幫助我?如何從主視圖按鈕onclick方法更新viewpager適配器?

回答

-1

有幾種方法可以實現這一點。

第一個選項更容易,但效率更低。

Override getItemPosition in your PagerAdapter like this: 

public int getItemPosition(Object object) { 
    return POSITION_NONE; 
} 

ViewPager PagerAdapter not updating the View

+0

檢查完整後http://stackoverflow.com/q/7263291/2624806 – CoDe