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

  • Home
  • SEARCH
  • 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 8234103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:21:57+00:00 2026-06-07T18:21:57+00:00

So I’m working on my hello world application in android/java, and elected to do

  • 0

So I’m working on my “hello world” application in android/java, and elected to do a sports app (which is strange…I don’t like sports…but whatever). So I set up my layout, allow users to ‘drill down’, so they can see the layout for Baseball, or MLB, or the Indians. Say a user selects ‘Indians’ from the MLB view. I update the tabs, potentially the color scheme, background, etc, and load the data for the ‘news’ and ‘players’ tabs (the latter of which is unique to team layouts). Unfortunately, api calls can sometimes take relatively long to complete, especially when the free API from ESPN is capped at 1 call per second. I do some significant caching already, but there’s no way I can guarantee that I won’t be loading both ‘news’ and ‘players’ for ‘Indians’ at the same time, so one of the requests will have to wait a full second to return.

So my solution is to have a data loading thread – the UI says ‘get me this data’, and does the UI work not contingent on the data being there. The question though is – once the data is returned from the data loader (as each piece comes back), how should it update or notify the UI appropriately? My current thought is:

UI thread:

OnSelectIndians()
{
  DataLoadThread.GetIndiansPlayers();
  DataLoadThread.GetIndiansNews();
  // UI stuff
}

OnPlayersLoaded(Array Players)
{
  if (layout == INDIANS_LAYOUT)  // Make sure we haven't changed layouts
  {
    foreach player in Players
      tab[PLAYERS].textview.text += player
  }
}

But this isn’t a problem I’ve had to deal with before. Is this the right way to go about it? Or is there a better/easier design I can use? I don’t particularly like requiring the UI thread to have a ‘on data returned’ method for every type of data I can request. My other loosely-formed idea is to create a lambda function in the UI code, which is passed to the data loader and executed in the data loading thread, so:

DataLoadThread.Queue(
    foreach player in GetIndiansPlayers()
        myView.tab[PLAYERS].textview.text += player;
);

But I think this is probably the worse route, as now we have 2 threads interacting with the UI. Any advice?

Edit: Okay I got it working using AsyncTask. Out of the box, it still has the problem listed above that I would have to create a new derived class for every type if data I load (so PlayerLoadTask, NewsLoadTask, StandingsLoadTask, etc etc). I also wanted was to have most of the logic visible during the call, so if I’m looking at the event code I know what its doing. Below is the working implementation – would appreciate any feedback on it, but I’ll accept the first answer below just the same.

abstract public class LoadDataHelper {
public LoadDataHelper(DataLoader dl, Object param) {
mDataLoader = dl;
mParam = param;
}

    abstract public LinkedList<String> LoadData();
    protected DataLoader mDataLoader;
    protected Object mParam;
}

abstract public class UpdateUIHelper {
    public UpdateUIHelper(MyActivity context) {
        mContext = context;
    }

    abstract public void UpdateUI(LinkedList<String> results);
    protected MyActivity mContext;
}

private class LoadDataTask extends AsyncTask<Void, Void, LinkedList<String> >   {
    private LoadDataHelper mLdh;
    private UpdateUIHelper mUih;

    LoadDataTask(LoadDataHelper ldh, UpdateUIHelper uih) {
        mLdh = ldh;
        mUih = uih;
    }

    @Override
    protected LinkedList<String> doInBackground(Void... params) {
        return mLdh.LoadData();
    }

    @Override
    protected void onPostExecute(LinkedList<String> results) {
        mUih.UpdateUI(results);
    }
}

//
// .....
// 

    LoadDataTask task = new LoadDataTask(new LoadDataHelper(mDataLoader, "football") {
                                            public LinkedList<String> LoadData() {                                                  
                                                return mDataLoader.LoadLeaguesFromSport((String)mParam);
                                            }
                                        }, 
                                        new UpdateUIHelper(this) {
                                            public void UpdateUI(LinkedList<String> results) {
                                                TextView tv = (TextView)findViewById(R.id.tv1);
                                                tv.setText("");
                                                for (String res : results) {
                                                    tv.append(res + "\n");  
                                                }
                                            }
                                        });
    task.execute();
  • 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-07T18:21:58+00:00Added an answer on June 7, 2026 at 6:21 pm

    Take a look at:

    1) AsyncTask
    http://developer.android.com/reference/android/os/AsyncTask.html

    The AsyncTask.onPostExecute will be executed in the UI thread.
    I think this is the most common technique to do background processing.

    2) runOnUIThread: If you are managing your own worker thread, you can use this in a worker thread to make sure code is run on the UI thread.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.