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 方法。如果我单击按钮,单选按钮在第一个位置禁用。之后我得到空指针异常。有什么想法吗纠正这个问题?我需要解决这个问题。有人帮我吗?

4

1 回答 1

-1

有几种方法可以实现这一点。

第一种选择更容易,但效率更低。

Override getItemPosition in your PagerAdapter like this:

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

ViewPager PagerAdapter 不更新视图

于 2013-07-26T07:32:49.680 回答