android viewpager适配器无法使用新视图进行更新

时间:2013-09-03 09:15:40

标签: android webview android-viewpager

我一直在使用viewpageAdapter,但是当我尝试使用新内容更新时,它似乎没有更新。但我从数据库中获取该内容。

这是我的ViewpagerAdapter类

public class ViewpagerAdapter extends PagerAdapter {
    Context context;
    ViewPager pager;

    public ViewpagerAdapter(Context ctx, ArrayList<String> list) {
        this.context = ctx;
        fullPr=list;
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return fullPr.size();
    }

    @Override
    public void notifyDataSetChanged() {
        // System.out.println("notifyDataSetChanged()");
        // TODO Auto-generated method stub
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == ((View) arg1);
    }

    @Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub
        WebView wb = new WebView(context);
        wb.setBackgroundColor(Color.parseColor("#F5EBC6"));
        wb.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
        // TextView wb=new TextView(context);
        wb.getSettings().setBuiltInZoomControls(true);
        wb.setPadding(10, 10, 10, 10);
        ((ViewPager) container).addView(wb, 0);
        String yourhtmlpage = "<html><body>" + fullPr.get(position)
                + "</body></html>";
        wb.loadDataWithBaseURL(null, yourhtmlpage, "text/html", "UTF-8",
                null);

        return wb;
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stubmatch_parent
        return null;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView((View) object);

    }

} 

这是onclick函数,我用它将新页面内容传递给适配器。这里的notifyDataSetChanged没有改变内容。

public void onClick(DialogInterface dialog, int item) {

    chooselangDesc = db.rawQuery("SELECT ZCON FROM ZPR WHERE  
ZPR1='"+ passLagID1 + "' AND ZLANGUAGE='"+ realLanguageID[item] + "'", null);
            if (chooselangDesc.moveToFirst()) {


prbyLang.add(chooselangDesc.getString(chooselangDesc
                        .getColumnIndex("ZCONTENT")));
                fullPr=null;
                fullPr=prbyLang;
    adapter_content = new ViewpagerAdapter(getApplicationContext(), fullPr);
                myPager.setAdapter(adapter_content);
                adapter_content.notifyDataSetChanged();
            }

            dialog.cancel();
        }

1 个答案:

答案 0 :(得分:0)

扩展FragmentStatePagerAdapter而非PagerAdapter

private class PageAdapter extends FragmentStatePagerAdapter {

        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

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

        //your code

        @Override
        public int getCount() {
            return IN_TOTAL_COUNT;
        }

    }

<强>更新

public ViewpagerAdapter(FragmentManager fm,Context ctx, ArrayList<String> list) {
                super(fm);
            }