Android ListView Async - android

I have a list that when I click on an item another screen should appear. I've successfully done this before but I've moved my list to Async - so the class extends AsyncTask not ListView.
I'm assuming this is why I'm getting errors on the following methods:
getListView();
getApplicationContext();
startActivity(in);
Any ideas?

You're going to need both a List and an AsyncTask. Use onProgressUpdate or onPostExecute to update your list from your AsyncTask.

Related

Refresh ListView when an AsyncTask is inside the Adapter

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.

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.

when listview starts loading new data the scrolling hangs it neither goes upward not downwards till data get loaded.?

when my listview starts loading new data the scrolling hangs it neither goes upward not downwards till data get loaded. Is there any way to slove this issue???
You can do data fetching task on seperate thread and Update the ListView in Handler.
Refer this-
http://negativeprobability.blogspot.in/2011/08/lazy-loading-of-images-in-listview.html
use progress dialog in your AsyncTask's onPostExecute and onPreExecute.
start it from onPreExecute & stop it in onPostExecute
after downloading completion, set the new data in adapter & use notifyDataSetChanged, put this code in onPostExecute

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

I am looking to refresh a ListView without reloading the page

I am looking to refresh a ListView without reloading the page. More precisely I have a service that is sending data for a ListView in an Activity, however the Activity loads long before the Service can get the data. So I need to be able to load/reload the ListView after the Activity has already loaded.
I found that notifyDataSetChanged only works if you use the add, insert, remove, and clear functions on the Adapter, so I ended up doing it the following way in a similar implementation:
An AsyncTask fetches all the data in doInBackground. Then, when finished I set the list adapter for the first time in onPostExecute. To let the user know that something is loading, I just put a TextView on top of the Listview and set its text to "Loading.." in onPreExecute and then make it invisible in onPostExecute when the data is ready.
If you need to refresh the data, you just execute the AsyncTask again.
I like this way because you are only setting the ArrayAdapter once (i.e. when you finally have all the data). Here is more on AsyncTask in case you need it. The docs have some nice example code.
Call notifyDataSetChanged() on the ListView's Adapter whenever you want to refresh it.
I would say to use IntentService instead of Service. By, using IntentService you will be able to send data to the background Service and also receive the updated data while firing a BroadCastReceiver to update your UI. Here is a complete example how you can achieve your task using an IntentService.

Categories

Resources