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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:53:27+00:00 2026-05-30T04:53:27+00:00

Retrieving stuff from the network cannot be done on the UI thread in android.

  • 0

Retrieving stuff from the network cannot be done on the UI thread in android.
I’m trying to get around this like so…

for(i=0;i<list.size();i++){

    //add a UI element

    new Thread(new Runnable(){
        public void run(){
            //retrieve online content
        }
    }).start();   
   LinearLayout.addView(MyView);

}

A similar example in the android docs

the part inside the thread accesses the list with the get() method. when “i” should be 0 to get the first item on the list “i” is in fact 1 and causes a OutOfBoundsException if there is only one item. I’m going to guess that I’ve got a problem with thread safty here. Is there an easy way to fix this problem?

  • 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-30T04:53:29+00:00Added an answer on May 30, 2026 at 4:53 am

    Read this http://developer.android.com/reference/android/os/AsyncTask.html to use AsyncTaskfor performing tasks in background instead of using Threads.

    AsyncTasks provide nice wrapper to perform background task and postExecute task(directly on UI thread) etc.

    EDIT:
    also from your code i m not sure that would be correct way… looks like you are running a thread for each element in your list.

    and is

    LinearLayout.addView(MyView);
    

    really dependent on the task you perform in background? If yes, do it once after you have retrieved all data from network (and inside the thread) because then only you will know that task has been done. if not then no reason to put it here..

    instead you must run a single thread(better async task) to retrieve from network, and show/update UI when the thread/task is over…

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

Sidebar

Related Questions

<% //Retrieving the Server list from dispatcher Collection<Server> svr = (Collection<Server>)request.getAttribute(stuff); ArrayList<String> serverIds =
When retrieving a lookup code value from a table, some folks do this... Dim
When retrieving packages with Cabal, I frequently get errors with this message: user error
I want to develop an android application which retrieving data from the Waze Live
I have a proxy application retrieving 'src' attributes cross domain from an xml list
When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should
When retrieving values from a DataRow is it better to use the column name
I am retrieving 3 fields from a database, and by default, only the username
We have a flash retrieving information from an XML file. Right now we are
I'm retrieving a float value from a mysql database on a php page (2.5,

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.