简体   繁体   中英

Android:How to add data to the ListView while downloading from the web

I am Downloading some data from web and displaying them into a list.

Now i want to add the item to the list when its downloaded means if whole data has 10 item if one is downloaded i want that data to add in the ListView so that list will become more attractive.

I am using AsyncTask to download the data and in post background i am binding collected data into the list using custom adapter.

Look into using Loaders . You will probably want to include the compatibility library in order to use that API and still support pre-3.0 devices.

Use this approach it will help you to do so.

Load your asyntask by

new MyAsynTaskname().execute(myarguments);

After that, make your Asyntask something like

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Handle your current downloading process in "onProgressUpdate" task.

Follow this Android Asyntask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM