title: Load Bitmaps/images in ListView Adapter
body: <p>I'm trying to add images in a ListView which has an ArrayAdapter. Fyi, the toList() is a conversion from iterator to a list of the given DBObject.</p>
  
  <p>I override the <code>View getView()</code> and set a textview and an image.</p>
  
  <pre><code>private static class EventAdapter extends ArrayAdapter&lt;DBObject&gt; {      
  
      public EventAdapter(Context context, int resource, Iterable&lt;DBObject&gt; events) {
          super(context, resource, toList(events));           
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
  
          View v = convertView;               
          LayoutInflater vi = LayoutInflater.from(getContext());                              
          v = vi.inflate(R.layout.adapter_event_list, null);
  
          DBObject event = getItem(position);
  
          if (event != null) {
  
              //Get the logo if any
              if( ((DBObject)event.get("events")).containsField("logo") ){      
  
                  String logoURL = ((DBObject)((DBObject)event.get("events")).get("logo")).get("0").toString();
                  ImageView eventLogo = (ImageView) v.findViewById(R.id.eventLogoList);
                  new setLogo().execute(logoURL, eventLogo);
  
              }               
              TextView title= (TextView) v.findViewById(R.id.eventTitleList);             
              title.setText( ((DBObject)event.get("events")).get("title").toString() );               
  
          }
  
          return v;
      }
  
      protected static &lt;T&gt; List&lt;T&gt; toList( Iterable&lt;T&gt; objects ) {
          final ArrayList&lt;T&gt; list = new ArrayList&lt;T&gt;();
          for( T t : objects ) list.add(t);
          return list;
      }
      //setLogo() method here. See below
   }
  </code></pre>
  
  <p>The text in the textview is fine. However the images are getting messed up. They seem to load in wrong places in the list. The route of the code is: 1)Get from the DB (async) 2)populate the ListView 3) while populating load each image(second async).</p>
  
  <p>Here is the <code>setLogo()</code> AsyncTask which is inside the <code>EventAdapter</code> above:</p>
  
  <pre><code>private class setLogo extends AsyncTask&lt;Object,Void,Bitmap&gt;{
  
          ImageView eventLogo = null;
  
          @Override
          protected Bitmap doInBackground(Object...params) {
              try{
                  Bitmap eventImage = downloadBitmap((String) params[0]);
                  eventLogo =  (ImageView) params[1];
                  return eventImage;
              }
              catch(Exception e){
                  e.printStackTrace();
                  return null;
              }
          }
  
          @Override
          protected void onPostExecute(Bitmap eventImage) {
              if(eventImage!=null &amp;&amp; eventLogo!=null){                    
                  eventLogo.setImageBitmap(eventImage);
              }
          }  
  
  }
  </code></pre>
  
  <p>I did so (using an Async) which I believe is the correct way to load images from urls. I saw this <a href="http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html" rel="nofollow noreferrer">post on multithreading</a> and from which I borrowed the <code>downloadBitmap()</code> method.</p>
  
  <p>As explained above the images are loaded in wrong places of the ListView. What can be a robust way to load them?</p>
  
  <p>Also the idea to pass the <code>v.findViewById(R.id.eventLogoList)</code> inside the AsyncTask is that the program will distinguish each adapter's ImageView but it seems it doesn't.</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>After following the problem that is causing this mix I found <a href="https://stackoverflow.com/a/3874639/1447885">this SO question</a>.</p>
  
  <p>I altered my code in order to check if the <code>if</code> is causing the problem.</p>
  
  <pre><code>//Get the logo if any               
  if( ((DBObject)event.get("events")).containsField("logo") ){                        
          String logoURL = ((DBObject)((DBObject)event.get("events")).get("logo")).get("0").toString();
          ImageView eventLogo = (ImageView) row.findViewById(R.id.eventLogoList);
  
          //new setLogo().execute(logoURL, eventLogo);   
  
          TextView title= (TextView) row.findViewById(R.id.eventTitleList);
          title.setText( "Shit happens" );    
  
      }
  </code></pre>
  
  <p>Let's say I have 40 items. The <code>Shit happens</code> is set on the fields that a <code>logo</code> field exists. If I scroll down/up the order changes and the text gets messed up. It is because the stack created inside the loop is small than the maximum of the list..I guess... I am still struggling.</p>
  
  <p>PS: I found <a href="https://github.com/nostra13/Android-Universal-Image-Loader" rel="nofollow noreferrer">this easy library</a> to load images asynchronously instead of DYI stuff.</p>
  
  <hr>
  
  <p><strong>Update 2</strong></p>
  
  <p>I added an else with a static url. Because of the time it take to the image to load they are still misplaced.</p>