I'm having problem with an AsyncTask in my app. I load some dat in doInBackground(), and with that data (contains a link to download an image) I fill a ListView. My idea was to fill the ListView in onPostExecute(), but I can't because the adapter uses a Http connection to download the image and show it.
So, if I put the adapter in onPostExecute() Android tells me that I can't make a network connection in UI main thread. But if I did it doInBackground() it tells me that only the original thread that created a view hierarchy can touch it views.
Any suggestion?
Related
I am fetching contacts and this work is going fine. But I am try to also show the contacts in bunch of 50 which is fetched, i.e. user don't have to wait until all contacts has been fetched.
I try to with Asynctask , And Thread also but when I go to notify the list adapter then error has came.
"The content of the adapter has changed but ListView did not receive
a notification. Make sure the content of your adapter is not modified
from a background thread, but only from the UI thread. Make sure your
adapter calls notifyDataSetChanged() when its content changes"
Make sure you update the adapter from the onProgressUpdate() method of the AsyncTask. The onProgressUpdate() can be invoked by calling the publishProgress() from inside yourdoInBackground(). doInBackground() runs in the Background Thread, while onProgressUpdate() runs in your UI thread.
what is meant by asynchronously loading data in activity or fragment in android?
This is my question. I searched everywhere. I'm not getting a generalized definition for this?. I can't get the term mentioned in android developer also.
Can anyone provide me the basic explanation of this term?
Asynchronous in Android mean that you do stuff while the user can interact with the User Interface (UI) : you are not blocking the UI while you are doing long stuff. So the user can still navigate, change activities or fragment and your data is still loading.
For data : you load it, parse it and do whatever you want in a NON-UI Thread (using AsyncTask eg) and then notify the UI, and display what you need to.
You have many possibilities to implement Asynchronous load in Android, and you have many different way to manage your request. I personnaly recommend using Retrofit if you need to use a Web API.
It means that you load your data in a separate thread than the UI thread. You launch your HTTP request for example in another thread and when it finished you notify the UI thread to refresh display.
This mean to load data in separate thread rather than load the data in main thread.Loading data in main thread may cause app to block
The AsyncTask class encapsulates the creation of a background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.
To use AsyncTask you must subclass it. AsyncTask uses generics and varargs. The parameters are the following AsyncTask .
An AsyncTask is started via the execute() method.
The execute() method calls the doInBackground() and the onPostExecute() method.
TypeOfVarArgParams is passed into the doInBackground() method as input, ProgressValue is used for progress information and ResultValue must be returned from doInBackground() method and is passed to onPostExecute() as a parameter.
The doInBackground() method contains the coding instruction which should be performed in a background thread. This method runs automatically in a separate Thread.
The onPostExecute() method synchronizes itself again with the user interface thread and allows it to be updated. This method is called by the framework once the doInBackground() method finishes.
I have read through the following article, it explained how a listView works, sometimes a view launches a asyncTask Thread, if the bitmap is not found in the lruCache/diskCache. But then that view gets recycled. Therefore when the asyncTask is finished, how does it know which view to populate ? is there a simple sample project you could also guide me to ?
The async task always tries to populate the view which you supplied as parameter. So when the screen orientation changes and the activity gets recreated the task may or may not finish decoding bitmap (depending on how far you read the turorial) but it will not show the image. Once the activity recreates a new async task will be started to load the image.
The recycled view is still there (as it is recycled). As long as you don't tell the AsyncTask that it should not populate to the now recycled view, it will show the wrong image in the view.
You need to implement an AsyncTask with a cancel mechanism that informs the AsyncTask that it should not populate anymore (or you null the reference to the view in the task and you check for null there).
Picasso, an image loading library, has a method that cancels previews requests for a view as soon as you create a new request which is basically what happens when you scroll within a listview and your view gets recycled.
My Android application is running very slow and lagging much. I have PHP API on my server and my application requests data through HTTP.
Though, the problem is that sometimes I should wait for few seconds before I can see the result. I have all calculations done in the main thread in onCreate (parsing XML, adding controls) and downloading data from HTTP server in AsyncTask.
How to optimize my program to make it faster? I want it to load activity first and only then, in background, download and parse data. How is it possible? Sorry for newbieship.
what did you mean by lagging ? Will you elaborate more on the issue.
One suggestion that remove parsing XML from OnCreate and move it to AsysnTask. The reason for this is as you are doing time consuming operation in UI Main thread which will impact the activity to be shown.
Create thread to perform HTTP related operations and parse the response on the same thread and while doing the parsing operation show dialog.
Dismiss the dialog when parsing got completed and then show the activity which you want to display.
In the doInBackground() method in AsyncTask add the data parsing and create the data objects then in onPostExecute update the ui elements.
The reason to do something like that is for the application to be responsive in all this time and just make small jobs on the ui thread so as not to freeze.
You can instatiate your views to a default state an add a progress somewhere on top to indicate that the activity is currently loading. For example you can create an empty ListView or a Button that cannot be selected and when the parsing is done then you should set the adapter to the list and make the button back to selectable again.
All this things can be implemented according to what you want the user to be able to do in the time of his waiting.
hi i am Using AsyncTask to download Xml's files,images from URl,after downloading xmls i am going to parse and displaying data in ListView of ImageView and TextView.i know how to update UI in
onPreExecute() onPostExecute(Void result),
if for Example i am downloading 100 xml's in doInBackground() method,i want to update List View for every 10 xml's download completed, i am using Handler to update List view by sending a message.its going to Force close due to
handlers,can any one suggest me how to do it...
Can any one tell me which is the way to solve my prob...
I wouldn't use a Handler if you're using a AsyncTask as it has already a build in mechanism to update the UI thread from the background thread.
You can pass updates to the UI thread using the onProgressUpdate() method. As onPostExecute() and onPreExecute() it is also executed on the UI thread. To pass an object to the UI thread you have to call publishProgress() within the doInBackground().