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?
Read this http://developer.android.com/reference/android/os/AsyncTask.html to use
AsyncTaskfor performing tasks in background instead of usingThreads.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
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…