Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7648335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:41:27+00:00 2026-05-31T10:41:27+00:00

There is a similar question here, Activity using ViewPager, PagerAdapter and AsyncTask causes a

  • 0

There is a similar question here, Activity using ViewPager, PagerAdapter and AsyncTask causes a blank view, but my problem is more than that.

I have a ViewPager that contains a ListView. This ListView is populated by an asyncTask. The listview displays the data, but not correctly. The first page is always blank, and the second page contains BOTH pages data.

I’ve simplified my asynctask to be sure tit wasnt something there that was causing the issue.

@Override
public Object instantiateItem(View pager, int position){
   // Log.d("PAGER", Integer.toString(position));
   ctx = pager.getContext();
   v = new ListView(ctx);
   new CreateArrayListTask().execute(URLS[position]);
   ((ViewPager)pager).addView(v, 0); 
   return v;
}

CreateArrayListTask

private class CreateArrayListTask extends AsyncTask<String, Void, ArrayList<String>> {               
   @Override
   protected ArrayList<String> doInBackground(String... params) {
     linkArray.add("Item 1");
     linkArray.add("Item 2");
     return linkArray;
   }

onPostExecute

protected void onPostExecute(ArrayList<String> linkArray) {
   ArrayAdapter<String> a = new ArrayAdapter<String>(ctx, android.R.layout.simple_list_item_1, linkArray);
   v.setAdapter(a);

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:41:28+00:00Added an answer on May 31, 2026 at 10:41 am

    The ListView on the second page is doubly populated precisely because of my answer given in the other question.

    You are assuming instantiateItem will be called only for the currently active page, whereas it will actually be called to preload for offscreen pages too. The reference v to the view will be overwritten by the second call to instantiateItem.

    Have a look at this example project to see one way how to do it with fragments: https://github.com/antonyt/ListFragmentViewPagerExample/tree/master/src/com/example/listfragment

    Activity:

    public class ListFragmentViewPagerActivity extends FragmentActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            ViewPager pager = (ViewPager) findViewById(R.id.pager);
            pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager()));
        }
    
        public class ExamplePagerAdapter extends FragmentPagerAdapter {
    
        public ExamplePagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public int getCount() {
            return 5;
        }
    
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new ExampleListFragment();
    
            // set arguments here, if required
            Bundle args = new Bundle();
            // args.put(...)
            fragment.setArguments(args);
    
            return fragment;
        }
        }
    }
    

    Fragment:

    public class ExampleListFragment extends ListFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         // normally you should inflate a view here and save references
        // using ListFragment default layout for this example
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    
        @Override
        public void onStart() {
            super.onStart();
            new CreateArrayListTask().execute();
        }
    
        private class CreateArrayListTask extends AsyncTask<String, Void, ArrayList<String>> {
        @Override
        protected ArrayList<String> doInBackground(String... params) {
            ArrayList<String> linkArray = new ArrayList<String>();
            linkArray.add("Item 1");
            linkArray.add("Item 2");
            return linkArray;
        }
    
        protected void onPostExecute(ArrayList<String> linkArray) {
            ArrayAdapter<String> a = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, linkArray);
            setListAdapter(a);
        }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a similar question here but it only covers some of the issues
I know there is a very similar question here but I was hoping to
There seems to be a similar question to this on here but with the
There is a similar question here but regarding the class path. ClassNotFoundException com.mysql.jdbc.Driver I've
There'a a similar question here but I couldn't make use of the answers in
There was a similar question asked here three years ago, but I want to
there is a similar question in here but for some reason I cannot make
Hi there (my first question here ;-) There might be similar questions but none
I am aware that there is a similar question here with no solution. I'm
Yes, there is a similar question here . However, that question doesn't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.