Note that there are some explanatory texts on larger screens.

plurals
  1. POActivity using ViewPager, PagerAdapter and AsyncTask causes a blank view
    primarykey
    data
    text
    <p>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.</p> <p>I decided to use ViewPager/PagerAdapter to handle the views and control the paging through days.</p> <p>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, <code>instantiateItem()</code> calls the API and sets up the empty listView. Then <code>BroadcastReceiver</code> catches the response, parses the data and displays it.</p> <p><strong>The problem I'm having is that the first view displayed is always blank.</strong> 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. <strong>Why? How to do get the first view to be populated without having to move over 2 swipes?</strong></p> <p>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.</p> <pre><code>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&lt;NameValuePair&gt; parameters = new ArrayList&lt;NameValuePair&gt;(); 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&lt;Map&lt;String, String&gt;&gt; items = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); for (int i = 0; i &lt; 20; i++) { Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); 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); } }; } </code></pre> <p>Thanks for taking the time to review this question!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload