Skip to content Skip to sidebar Skip to footer

Illegalstateexception: Adapter's Content Without Calling Pageradapter#notifydatasetchanged

I did search all the question related to this but could not find any solution here is my code. private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; M

Solution 1:

This is happening because once you get the data from AsyncTask you are not refreshing your adapter. This is how you should fix it.

Add pagerAdapter.notifyDataSetChanged(); in onPostExecute method.

Solution 2:

There was a silly mistake thanks to @Rohit5k2 its solved. I just had to pagerAdapter.notifyDataSetChanged.. It might be useful for some one who might want to populate their pagerAdapter based from server.

privateToolbar toolbar;
privateTabLayout tabLayout;
privateViewPager viewPager;
MyFragmentPagerAdapter pagerAdapter;
ArrayList<String> titles = newArrayList<String>();

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scrollable_tabs);
    newJSONAsyncTask()
    .execute("Your_Url");

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);








}

classJSONAsyncTaskextendsAsyncTask<String, Void, Boolean> {

    @OverrideprotectedvoidonPreExecute() {
        super.onPreExecute();

    }


    @OverrideprotectedBooleandoInBackground(String... urls) {
        try {

            HttpGet httppost = newHttpGet(urls[0]);
            HttpClient httpclient = newDefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONArray jsor = newJSONArray(data);

                for(int i =0; i <jsor.length();i++){

                    JSONObject obj = jsor.getJSONObject(i);


                    titles.add(obj.getString("Location"));



                }
                returntrue;
            }

            // ------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        returnfalse;
    }

    protectedvoidonPostExecute(Boolean result) {

        if (result == false) {

        } else {
              viewPager = (ViewPager) findViewById(R.id.viewpager);
                pagerAdapter = newMyFragmentPagerAdapter(getSupportFragmentManager());
                viewPager.setAdapter(pagerAdapter);
                tabLayout = (TabLayout) findViewById(R.id.tabs);
                tabLayout.setupWithViewPager(viewPager);
            pagerAdapter.notifyDataSetChanged();
        }
    }
}

publicclassMyFragmentPagerAdapterextendsFragmentPagerAdapter{



    /** Constructor of the class */publicMyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }




    /** This method will be invoked when a page is requested to create */@OverridepublicFragmentgetItem(int arg0) {

        MyFragment myFragment = newMyFragment();
        Bundle data = newBundle();
        data.putInt("current_page", arg0);

        myFragment.setArguments(data);

        return myFragment;

    }

    /** Returns the number of pages */@Overridepublic int getCount() { 

        return titles.size();

    }

    @OverridepublicCharSequencegetPageTitle(int position) {        
        return titles.get(position) ;

    }





}

}

Post a Comment for "Illegalstateexception: Adapter's Content Without Calling Pageradapter#notifydatasetchanged"