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();
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.
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.
I have a ListView and a Custom Adapter. The Custom Adapter implements an AsyncTask to load some images from a server. Everything works fine when the activity is created. However, the ListView has a Load Button to load more rows, each row contains an image. The new data (images) are added properly to the ListView when de Load Button is pressed. However, the ListView is not refreshed despite notifyDataSetChanged is called. I have to move the scoll to refresh the ListView and the new data are displayed. The problem is that the new images are loaded in the AsyncTask inside the Adapter and when notifyDataSetChanged is called in the MainActivity, the AsyncTask is not finished yet. So, my question is: Is there any way to refresh the ListView once the AsyncTask is finished? Calling notifyDataSetChanged inside the Adapter or another alternative?
In your asynctask onPostExecute it should be something like this:
#Override
protected void onPostExecute(String result) {
((MainActivity) getActivity).notifyDataChanged();
}
Depending on how your code is organized you will call notifyDataChanged() in some way similar to this one.
So, my question is: Is there any way to refresh the ListView once the AsyncTask is finished?
Yes you can do that :
Pass the ListView to the AsyncTask,
override the onPostExecute() method,
call notifyDataChanged in the onPostExecute() method.
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.
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.