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 7648193
In Process

The Archive Base Latest Questions

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

I’m trying to build an Activity that will serve as a Calendar Day View.

  • 0

I’m trying to build an Activity that will serve as a Calendar Day View. When the user swipes left or right, they’ll go to tomorrow or yesterday and so on through the calendar.

I decided to use ViewPager/PagerAdapter to handle the views and control the paging through days.

As part of setting up the Day View, the app will go to my API and request any appointments for that day. The appointments will be returned and displayed for that day. For retrieving data from the API, I’m using an AsyncTask. So basically, instantiateItem() calls the API and sets up the empty listView. Then BroadcastReceiver catches the response, parses the data and displays it.

The problem I’m having is that the first view displayed is always blank. The views to the left or right, are populated and if I go two positions in either direction, enough for the original view to get destroyed, then go back to the original view, it has data. Why? How to do get the first view to be populated without having to move over 2 swipes?

Here’s my activity. I’m not currently parsing the data coming back from the API yet, just simulating with a list of strings in the mean time.

public class MyPagerActivity extends Activity {

    private ViewPager myPager;
    private static int viewCount = 1000;
    private Context ctx;
    private MyPagerAdapter myAdapter;

    private static final String tag = "MyPagerActivity";

    private static final String apiAction = "getAppointmentsForDate"; 
    private static final String apiUri = "https://myAPI.com/api.php";
    private static final String resource = "appointments";
    private static final String action = "getappointmentsfordate";
    private static final String date = "20120124";
    private ProgressDialog progress;
    private SharedPreferences loginPreferences;
    private SharedPreferences.Editor loginPreferencesEditor;
    private ListView v;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ctx = this;

        myAdapter = new MyPagerAdapter();
        myPager = (ViewPager) findViewById(R.id.myPager);
        myPager.setAdapter(myAdapter);
        myPager.setCurrentItem(500);
    }

    private class MyPagerAdapter extends PagerAdapter {


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

        @Override
        public Object instantiateItem(View collection, int position) {

            try {
                Log.d(tag, "trying http connection");
                loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
                loginPreferencesEditor = loginPreferences.edit();
                String authToken = loginPreferences.getString("authToken", "");
                String staffOrRoomsId = loginPreferences.getString("staffOrRoomsId", "");
                String staffOrRoomsIdName = loginPreferences.getString("staffOrRoomsIdName", "");

                HttpPost apiRequest = new HttpPost(new URI(apiUri));
                List<NameValuePair> parameters = new ArrayList<NameValuePair>();
                parameters.add(new BasicNameValuePair("authToken", authToken));
                parameters.add(new BasicNameValuePair("resource", resource));
                parameters.add(new BasicNameValuePair("action", action));
                parameters.add(new BasicNameValuePair("date", date));
                parameters.add(new BasicNameValuePair(staffOrRoomsIdName, staffOrRoomsId));
                apiRequest.setEntity(new UrlEncodedFormEntity(parameters));

                RestTask task = new RestTask(ctx, apiAction); 
                task.execute(apiRequest); 
                //Display progress to the user 
    //            progress = ProgressDialog.show(ctx, "Searching", "Waiting For Results...", true); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 

            Log.d(tag, "Creating another view! Position: " + position);

            myPager.setTag(collection);

            v = new ListView(ctx);
            ((ViewPager) collection).addView(v, 0);
            return v;
        }

        @Override
        public void destroyItem(View collection, int position, Object view) {
            Log.d(tag, "Destroying position: " + position + "!");
            ((ViewPager) collection).removeView((ListView) view);
        }

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

        @Override
        public void finishUpdate(View arg0) {}

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

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

        @Override
        public void startUpdate(View arg0) {}
    }

    @Override 
    public void onResume() { 
        super.onResume(); 
        registerReceiver(receiver, new IntentFilter(apiAction));  
    } 

    @Override 
    public void onPause() { 
        super.onPause(); 
        unregisterReceiver(receiver); 
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() { 
        @Override 
        public void onReceive(Context context, Intent intent) { 
            //Clear progress indicator 
            if (progress != null) { 
                progress.dismiss(); 
            }

            Log.d(tag, "Broadcast received");

            String[] from = new String[] { "str" };
            int[] to = new int[] { android.R.id.text1 };
            List<Map<String, String>> items = new ArrayList<Map<String, String>>();
            for (int i = 0; i < 20; i++)
            {
                Map<String, String> map = new HashMap<String, String>();
                map.put("str", String.format("Item %d", i + 1));
                items.add(map);
            }
            SimpleAdapter adapter = new SimpleAdapter(ctx, items, android.R.layout.simple_list_item_1, from, to);
            v.setAdapter(adapter);
        } 
    };
}

Thanks for taking the time to review this question!

  • 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:39:14+00:00Added an answer on May 31, 2026 at 10:39 am

    By default, a ViewPager not only loads only the currently visible page, but also the page to either side. This is the offscreenPageLimit. The minimum value is 1.

    instantiateItem will be called for each page it wants to create. In your implementation of instantiateItem, you are starting an AsyncTask and assigning a value to v. Only the last call to instantiateItem (i.e. the last page upto the offscreenPageLimit) will have the correct assignment of v, and so the first page will not be populated initially.

    You cannot rely on the last call to instantiateItem to point you to the currently active page. I would switch to using a Fragment for each page, which maintains its own HTTP request and ListView.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.