1

我一直在使用 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();
        }
4

1 回答 1

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);
            }
于 2013-09-03T09:21:16.143 回答