Call notifyDataSetChanged after each item I receive in AsyncTask - android

I use an AsyncTask and on doInBackground() I have a loop where I'm doing a lot of network calls. Each call gives me a new data object that I want to add it to my listView.
I know that I can call notifyDataSetChanged() on onPostExecute() and update my list with all my items but I'm wondering if its possible to somehow call notifyDataSetChanged() when I receive a new item and update my listView the same moment and not to have to wait until all the work on doInBackground finishes.
If there isn't a way I can do this with AsyncTask then whats the proper way to deal with something like that?
Thank you

Call publishProgress(...) from doInBackground(...) and override onProgressUpdate(...) in your AsyncTask and call notifyDataSetChanged() there.
Calling publishProgress(...) from doInBackground(...) instructs the AsyncTask to call it's onProgressUpdate(...) on the main UI thread, hence you can update the ListView from there.

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.

Is it right way to update List View custom adapter in onProgressUpdate in Android?

I have Created Model and Calling Asynchronous task in MainActivity onCreate function to update adapter. It is working fine but i have doubt whether can i update adapter in Asynchronous onProgressupdate function.
No, you need to update the adapter in postExecute of the AsyncTask.

list view java.lang.IllegalStateException even after calling notifyDataSetCanged();

java.lang.IllegalStateException: 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.
my list-view contains huge data`s
am calling the bellow code using handler when the data is changed in the list
listviewAdapter.notifyDataSetChanged();
but i get the same error some times and not every time i load it
thanks in advance
the question is not where the adapter update, it should be: the time the adapter update.
it look likes: your data bind to the adapter has be changed, and the ui have be updated, so this time, the system find the data changed but can't invoke the notifyDataSetChanged.
so, suggestion when you change the adapter's data, invoke notifyDataSetChanged immediately,
or
dont directed change the adapter's data in your thread, you should send the data to the hanlder, and in the handler using the data to replace or change the adapter's data and notifyDataSetChanged.
so anyway, suggestion get the data can run in background thread, and update the data , notifyDataSetChanged used in ui thread.
the error is quite clear. You have not to call listviewAdapter.notifyDataSetChanged(); from a thread different of the UI thread.
Modify the arguments of the adapter in runOnUIThread() or use a handler to notify the adapter that the dataset has changed.

notifyDataSetChanged() doesn't refresh my view

I'm doing this in an AsyncTask, everything working fine but my view is not directly refreshed,
but if i just scroll a bit on my listView the data are refreshed
ArrayList<Project> listProject = taskLiteApplication.getListProjectWithStatus(Integer.parseInt(status[0]));
//set adapter
projectBaseAdapter.replaceData(listProject);
//display projects on screen
projectBaseAdapter.notifyDataSetChanged();
I try to call refreshDrawableState() on my listView and it doesn't work as well
and invalidate() too
Any UI updates from an AsyncTask should be done in the onPreExecute and the onPostExecute methods of the AsyncTask class.
So in your case, after your AsyncTask is complete, you want to update the UI. Therefore you should override the onPostExecute method of your AsyncTask , and in that method call projectBaseAdapter.notifyDataSetChanged();

Problems with AsyncTask and updating UI with ProgressUpdate and Listeners

I am getting a "CalledFromWrongThreadException" error when I try to update a TextView (via a listener) from an AsyncTask onProgressUpdate.
If I try to update the same TextView from onPostExecute everything works.
I have been testing using code based on
https://github.com/commonsguy/cw-android/tree/master/Service/WeatherAPI
(with a small mod that does an onProgressUpdate in the doInBackgroundMethod, and adds the onProgressUpdate override)
Any suggestion to fixes would be most appreciated.
Are you calling onProgressUpdate() from your code? You shouldn't do it. Use publishProgress() method.
onProgressUpdate doesn't run on UI thread, so you can't access views from this method. If you want to update progress, you should find a way to synchronize your AsyncTask with your activity. A way that I'm using is to create an interface with methods like onBegin, onUpdate and onFinish. You should implement this interface in your main activity class. Then you should have an instance of your activity inside your AsyncTask. In the onProgressUpdate method you just call the onUpdate method in your activity and update the layout. Hope I've explained it clear enough.

Categories

Resources