Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.43 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. What to return for an Async Task
  2. getImage(); //Gets an image from the internet for an imageview
  3. getJson();  //Where the app goes an parses a JSON object for a lazy load listview.
  4.        
  5. private class DownloadTask extends AsyncTask<String, Void, Object> {
  6. protected Object doInBackground(String... args) {
  7.     Log.i("MyApp", "Background thread starting");
  8.  
  9.     try {
  10.         ImageView i = (ImageView) findViewById(R.id.currdoodlepic);
  11.         Bitmap bitmap = BitmapFactory
  12.                 .decodeStream((InputStream) new URL(imageURL)
  13.                         .getContent());
  14.         i.setImageBitmap(bitmap);
  15.     } catch (MalformedURLException e) {
  16.         e.printStackTrace();
  17.     } catch (IOException e) {
  18.         e.printStackTrace();
  19.     }
  20.     getJson("all");
  21.  
  22.     return "replace this with your data object";
  23. }
  24.        
  25. void postExecute(Object result); // AsyncTask method
  26.        
  27. ImageView mChart = (ImageView) findViewById(R.id.imageview);
  28. String URL = "http://www...anything ...";
  29.  
  30. mChart.setTag(URL);
  31. new DownloadImageTask.execute(mChart);
  32.        
  33. public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
  34.  
  35. ImageView imageView = null;
  36.  
  37. @Override
  38. protected Bitmap doInBackground(ImageView... imageViews) {
  39.     this.imageView = imageViews[0];
  40.     return download_Image((String)imageView.getTag());
  41. }
  42.  
  43. @Override
  44. protected void onPostExecute(Bitmap result) {
  45.     imageView.setImageBitmap(result);
  46. }
  47.  
  48.  
  49. private Bitmap download_Image(String url) {
  50.    ...
  51. }