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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:43:17+00:00 2026-06-07T23:43:17+00:00

I have an Activity which scrapes a webpage in a CustomLoader. When loader finished

  • 0

I have an Activity which scrapes a webpage in a CustomLoader.
When loader finished I want to update the contents of 3 tabs with the data retrieved.

I’m using the sample code provided by android dev samples to implement the Viewpager/Tabs/Fragments on the activity.

When the fragment is created for the tab the onCreateView is correctly called, all the widgets on the view are correctly located and mapped to the variables.

However, when I attempt to find the fragment from the Activity and call a method on the fragment to update its contents the variables are null. Furthermore, calling getView also returns null – The instance of the fragment I’m retrieving from the TabsAdapter is not the correct instantiated instance ?

I’ve cut the code down to a single tab fragment, code in question is the call from onLoaderFinished to update the fragment.

All 3 tab fragments will be populated with data from a single loader, hence the loader being on the activity, not on the fragments. I just need a way to tell the fragments to paint their new data.

public class InfoBloodStocksActivity extends SherlockFragmentActivity 
    implements LoaderManager.LoaderCallbacks<BloodStocksLoaderResponse> {

    TabHost mTabHost;
    ViewPager  mViewPager;
    TabsAdapter mTabsAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.info_bloodstocks_tabpager);


        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mViewPager = (ViewPager)findViewById(R.id.infoBloodStocksTabPager);
        mViewPager.setOffscreenPageLimit(3);

        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

        mTabsAdapter.addTab(mTabHost.newTabSpec("Daily stock").setIndicator("Daily stocks"),
                InfoBloodStocksPageFragment.class, null);

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }

        loadStockDetailsFromWebsite();

    }



    private void loadStockDetailsFromWebsite() {
        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.

        getSupportLoaderManager().initLoader(0, null, this);
    }


    @Override
    public Loader<BloodStocksLoaderResponse> onCreateLoader(int arg0, Bundle arg1) {
        // This is called when a new Loader needs to be created.  This
        // sample only has one Loader with no arguments, so it is simple.
        return new BloodStocksCustomLoader(this);
    }



    @Override
    public void onLoadFinished(Loader<BloodStocksLoaderResponse> loader, BloodStocksLoaderResponse response) {
        if (response.isNewStocksLoaded()) {

            InfoBloodStocksPageFragment fragment = (InfoBloodStocksPageFragment) mTabsAdapter.getItem(0);

            fragment.setNewImage(response.getDailyStocksImageURL());
        }
    }


    @Override
    public void onLoaderReset(Loader<BloodStocksLoaderResponse> arg0) {
        // TODO Auto-generated method stub

    }







    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost.  It relies on a
     * trick.  Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show.  This is not sufficient for switching
     * between pages.  So instead we make the content part of the tab host
     * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
     * view to show as the tab content.  It listens to changes in tabs, and takes
     * care of switch to the correct paged in the ViewPager whenever the selected
     * tab changes.
     */
    public static class TabsAdapter extends FragmentPagerAdapter
            implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
            super(activity.getSupportFragmentManager());

            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {

            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);

            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        @Override
        public void onTabChanged(String tabId) {
            int position = mTabHost.getCurrentTab();
            mViewPager.setCurrentItem(position);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // Unfortunately when TabHost changes the current tab, it kindly
            // also takes care of putting focus on it when not in touch mode.
            // The jerk.
            // This hack tries to prevent this from pulling focus out of our
            // ViewPager.

            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }  
}

I’m calling setNewImage from the activity.

public class InfoBloodStocksPageFragment extends SherlockFragment {

    private ImageView mImageView;
    private ProgressBar mProgress;
    private TextView mTitle;
    private TextView mDescriptionText;

    private String mImageURL;

    private ImageLoader mImageLoader = ImageLoader.getInstance();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.info_bloodstocks_page, container, false);

        mImageView = (ImageView)view.findViewById(R.id.bloodStocksPageImage);
        mProgress = (ProgressBar)view.findViewById(R.id.bloodStocksPageProgress);
        mTitle = (TextView)view.findViewById(R.id.bloodStockPageTitle);
        mDescriptionText = (TextView)view.findViewById(R.id.bloodStocksPageText);

        mProgress.setVisibility(View.VISIBLE);
        mDescriptionText.setVisibility(View.GONE);

        return view;
    }


    public void setNewImage(String imageURL) {

        // why is mImageView null here ?
        // why does getView() return null here ?

        View view = getView();
        mImageView = (ImageView)view.findViewById(R.id.bloodStocksPageImage);

        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.loading_stock_figures)
            .cacheOnDisc()
            .imageScaleType(ImageScaleType.EXACT)
            .build();

        mImageLoader.displayImage(imageURL, mImageView, displayImageOptions, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted() {
                mProgress.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(FailReason failReason) {
                String message = null;
                switch (failReason) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }

                mProgress.setVisibility(View.GONE);
                mImageView.setImageResource(android.R.drawable.ic_delete);
            }

            @Override
            public void onLoadingComplete() {
                mProgress.setVisibility(View.GONE);
                Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
                mImageView.setAnimation(anim);
                anim.start();
            }

            @Override
            public void onLoadingCancelled() {
                // Do nothing
            }
        });

    }


}
  • 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-06-07T23:43:19+00:00Added an answer on June 7, 2026 at 11:43 pm

    I’ve just started using http://square.github.com/otto/ for doing this. The documentation on the site should be enough to get you started with it.

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

Sidebar

Related Questions

I have an Activity which I want to be finished when user rotates the
I have an activity which I want to use to display data sent from
I have an Activity which uses a Fragment . I simply want to pass
I have an Activity which looks up data from the web in its onCreate
I need to have Receive activity which can receive my custom data. I found
I have an activity which I want to show ListView with list of topics,
I have an activity which I'm using LayoutInflater in order to add views and
I have one activity which download data from the web. In middle of the
I have an activity which shows an image (ViewCollection.java). I want to only create
I have an activity which shows 3 random images from JSON data off the

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.