I ended up getting this solved as follows.

I basically just created a new adapter every time the data set needed
to change and replaced the adapter for the view pager.

InternalContentAdapter adapter = new InternalContentAdapter();
adapter.loadContents(someContentList);
this.internalContentPager.setAdapter(adapter);

This is my pager adapter.  I'm using internal web views as my views.
This probably isn't the best explanation (or implementation) but it
works for me.  Let me know if you have any questions.

/**
     * Pager Adapter to handle internal web view pages.  This drives
the swipe navigation for the internal content.
     *
     * @author btate
     *
     */
    private class InternalContentAdapter extends PagerAdapter{

                /** The content objects represented by this pager. */
                private List<Content> contents;

                /** The starting position. */
                private int startPosition = 0;

                /** The starting content id. */
                private String startContentId = null;

                /** Flag for whether or not to set the starting position. */
                private boolean setPrimary = true;

                @Override
                public int getCount() {
                        // Send back the contents size
                        return this.contents.size();
                }


            /**
             * Create the page for the given position.  The adapter is
responsible
             * for adding the view to the container given here, although it
only
             * must ensure this is done by the time it returns from
             * {@link #finishUpdate()}.
             *
             * @param container The containing View in which the page will be
shown.
             * @param position The page position to be instantiated.
             * @return Returns an Object representing the new page.  This
does not
             * need to be a View, but can be some other container of the
page.
             */
                @Override
                public Object instantiateItem(ViewGroup container, int 
position) {


                        // Load up the page
                InternalWebView page = new InternalWebView(ctx, repo);
        
page.loadInternalPage(this.contents.get(position).getContentId());
                page.setInternalWebViewListener(getListener());


                //this.parentActivity.registerForContextMenu(page);

                // Need to add this without the position
                container.addView(page);



                return page;
        }

            /**
             * Remove a page for the given position.  The adapter is
responsible
             * for removing the view from its container, although it only
must ensure
             * this is done by the time it returns from {@link
#finishUpdate()}.
             *
             * @param container The containing View from which the page will
be removed.
             * @param position The page position to be removed.
             * @param object The same object that was returned by
             * {@link #instantiateItem(View, int)}.
             */
                @Override
                public void destroyItem(View container, int position, Object 
view) {
                        ((ViewPager) container).removeView((InternalWebView) 
view);
                }





                @Override
                public boolean isViewFromObject(View view, Object object) {
                        return view==((InternalWebView)object);
                }

            /**
             * Called when the a change in the shown pages has been
completed.  At this
             * point you must ensure that all of the pages have actually been
added or
             * removed from the container as appropriate.
             * @param container The containing View which is displaying this
adapter's
             * page views.
             */
                @Override
                public void finishUpdate(ViewGroup container) {

                        // Set primary flag set to false after all data loaded
                        if(this.setPrimary)
                                // Set the current item
                                
internalContentPager.setCurrentItem(this.startPosition);

                        this.setPrimary = false;

                }


                @Override
                public void restoreState(Parcelable arg0, ClassLoader arg1) {}

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

                @Override
                public void startUpdate(ViewGroup container) {



                }

                /**
                 * Set the contents for the view pager.
                 *
                 * @param contents      A List of Content objects to be loaded.
                 * @param startPosition The starting position
                 */
                public void setContents(List<Content> contents, int 
startPosition){
                        // Tells it to set the primary page
                        this.setPrimary = true;

                        // Set the contents
                        this.contents = contents;


                        // Figure out the start position for the finished 
update function
                        if(this.startContentId != null){
                                for(Content tmp : this.contents){

                                        if(this.startContentId != null &&
this.startContent.equals(tmp.getContentId()))
                                                this.startPosition = 
this.contents.indexOf(tmp);
                                }
                        }

                        this.notifyDataSetChanged();
                }
}

On Feb 7, 12:13 am, HeneryH <[email protected]> wrote:
> tatebn, did you ever solve this problem?  I have been going crazy trying to
> dynamically add views to a viewpager based on database items.
>
> If I find one more sample with three statically defined views each adding a
> textview with the index I will go crazy!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to