0

In my application I am fetching the data from a web service in XML format, and parsing it and showing the data in listview. The problem is that if the web service contains 5000 objects then it takes a lot of time to display the data.

Can it be possible to show some data in listview and fetch the data at the same time at the end of the list.

Please provide me some sample code.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Arun Joshi
  • 170
  • 1
  • 1
  • 10
  • What have you tried? We don't write code for free, we fix and guide programmers. – Juan Cortés Jun 04 '12 at 09:23
  • Why don't you use [Fedors Lazy Load](http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview)? – Lalit Poptani Jun 04 '12 at 09:24
  • If your webservice supports some form of pagination, then I'd recommend looking at @Commonware's [`EndlessAdapter`](https://github.com/commonsguy/cwac-endless). If not, you can still use it to do the parsing in chuncks and append new items to the list as the user hits the bottom, but whether it makes sense depends on what the most time consuming step in your logic is. If it's the downloading of one humongous xml file, then there's probably little point. – MH. Jun 04 '12 at 09:46
  • @MH. Thanks for your valuable suggestion, i am using asynctask for that fetching and parsing but not know that how to do same thing in chunks. can you please specify in detail what you want to convey. – Arun Joshi Jun 04 '12 at 10:19

2 Answers2

0

By Using AsyncTask you can do this easily as each object is being fetched can be shown in listview using publishProgress() method while also updating user about what percentage of data hasbeen loaded.

Update:

By the way according to your situation the tool below which is developed by commonsware https://stackoverflow.com/users/115145/commonsware will suits you best...

https://github.com/commonsguy/cwac-endless

cwac-endless: Provides the EndlessAdapter, a wrapper for an existing ListAdapter that adds "endless list" capability. When the user scrolls to the bottom of the list, if there is more data for this list to be retrieved, your code gets invoked in a background thread to fetch the new rows, which then get seamlessly attached to the bottom of the list.

Community
  • 1
  • 1
Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40
  • What about the adpater of list view, its is showing the data in listview. – Arun Joshi Jun 04 '12 at 10:23
  • see this link that describes how adapter will update when new data arrives........ http://stackoverflow.com/questions/4540754/add-dynamically-elements-to-a-listview-android – Umar Qureshi Jun 05 '12 at 07:40
  • Thanks for your guidance.. but my concern is that how i put a limit in adapter to fetch only 20 items at a time. and how to get another items orderwise.. please help me.. – Arun Joshi Jun 05 '12 at 10:00
  • adapter by itself does not fetches data, it shows whatever you pass to it through an array or arrylist while intializing so if you put 20 items in arraylist, the adapter will show 20 and when you update arraylist and call notifyDataSetChanged() on adapter then it will update your list with new items. you can start listview with arraylist having no item and then in asynctask put each fetched item in same arraylist(passed in adapter) and when 20 items fetched then call notifyDataSetChanged() on adapter to update listview – Umar Qureshi Jun 05 '12 at 11:20
  • I am Using Asynctask for fetching and parsing the object and showing in custom list. but i am totally confused to how to use endless adapter in this scenario. kindly Can you Please provide me some sample code of using Endless Adpater with Custom Adpater in app. thanks – Arun Joshi Jun 05 '12 at 14:02
0

If you use convertView in your ListAdapter´s getView method it should not matter how many items you have in the list since the Views are beeing reused.

If your Listadapter takes an array of som sort you could add items to the array continuosly and call

mListAdapter.notifyDataSetChanged();

every time new data is added to the list.

Zelleriation
  • 2,834
  • 1
  • 23
  • 23
  • I am fetching the data and using AsyncTask for that, but don know how to use nofifyDataSetChanged in case of Fetching and parsing the data and setting them in list and same time fetching... – Arun Joshi Jun 04 '12 at 10:22