Android Refresh UI data from AsyncTask - android

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().

Related

Fetch contacts and show simultaneously

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.

Asynchronously load data in android

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.

Activity's UI freezes on AsyncTask's onPostExecute?

I used AsyncTask to request for a large chunk of data. When received, the data is processed inside the onPostExecute method. It may take a while to process the data.
Based from my understanding, AsyncTask is asynchronous and is independent from the UI.
Why does my Activity freezes onPostExecute?
Is it normal for an Activity to freeze if processing inside the onPostExecute method is too long?
How do I make it such that my Activity won't freeze onPostExecute?
You should do all your datasource operations like(database , network ,parsing the response.etc) in doInBackground method.
If you want update any ui updation in async task the use onProgressUpdate
I think you are performing any parsing operations in onPostExecute. try getting filtered or parsed data (lighter data) in onPostExecute.
Why does my Activity freezes onPostExecute?
According your post, you perform some time consuming operation on
PostExecute method. On PostExecute is running on UI thread, so it's
okay that you UI is freezed.
Is it normal for an Activity to freeze if processing inside the onPostExecute method is too long?
Yes, it's. You should perform long operation in doInBackground method
(non-UI thread)
How do I make it such that my Activity won't freeze onPostExecute?
Try to transfer your long time operation to doInBackground method and
in PostExecute just update UI according response, which you get after
operations in doInBackground method.
see Long Running Task You Need to Bind in doInBackground here you cannot Bind UI control Like Button, ImageView etc. After completion of doInBackground in onPostExecute you can Bind All Require Control, make sure here[onPostExecute] you not runnning Another task.
Make sure you are using doInBackground method in your AsynTask method.

Network connection and UI views change in same method

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?

How to make the Main thread wait, when i dont know when will Async task finish the job.?

I am parsing the xml file containing some names using the Async task and populating these names to the listview again via the main thread. But whats happening in my case is, when the Async task is still running, the main thread is already populating the names to the listview which is resulting in no items on the listview. Should i make the main thread wait until the Async task finish the job or is there any other way to solve this prob.? if yes how can i make the main thread wait when i don't know that how long the Async task might take to finish.?
If you want to complete the AsycTask then you can use .get() method from AsyncTask. (Which will block the MainUiThread)
But I think the best approach is without blocking MainUiThread you have to start ProgressDialog on onPreExecute() and after populating ListView in onPostExecute() of AsyncTask finish the ProgressDialog in onPostExecute().
If you have used AsyncTask, then do the xml fetch in doInBackground() and update all your UI elements of the listview inside the onPostExecute(), this runs on the main UI thread and is automatically after doInBackground(). This way you dont have to explicitly make the UI thread wait
You don't want to block your main thread and wait for the async task (this would defeat the purpose of the async task). Instead, the async task should trigger the update when it is finished in the onPostExecute method.
You may also want to have a look at the more recent Loader design.
- First of all Main thread in Android is the Dedicated UI thread.
- What you need to do is to fill the List by parsing the xml with the names and then proceed to Displaying it on the ListView.
- You can use CoundDownLatch from java.util.concurrent package to fill in the data into the list before showing it on the ListView.
- Use the method like await() and countDown() of CoundDownLatch to do this.
use AsyncTask.get() to wait until AsyncTask finish.
NOTE : this will stop execution of Main Thead until result not retrieved from AsyncTask
String str_result= new RunInBackGround().execute().get();
Log.e("","show this");
RunInBackGround is your AsyncTasc class name.
First will run async task and when it finished will show the bellow message!

Categories

Resources