如何从主视图按钮onclick方法更新viewpager适配器?

时间:2013-07-26 06:24:46

标签: android radio-button android-viewpager android-adapter

  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 for listview.Clear是适配器外部的onclick方法。如果我单击按钮,单选按钮被禁用第一个到位置。之后我得到空指针异常。在那里有什么想法来纠正这个问题吗?我需要解决这个问题。有人帮助过我吗?

1 个答案:

答案 0 :(得分:-1)

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

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

Override getItemPosition in your PagerAdapter like this:

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

ViewPager PagerAdapter not updating the View