title: Android - Set the Source of an ImageView inside a custom listView adapter from the getView function does not work properly
body: <p>Apologies for the long title. </p>
  
  <p>I am building an Android application where an activity contains a custom listView. In the layout of my custom listView, I have an image view and three textViews. I am fetching the data from the database and I have no issues with the row data as they're accurate. However, I have issues with displaying the ImageViews. </p>
  
  <p>What I do to populate the imageView is that I get the imagePath stored from the database, parse that URL (local server only), then set the ImageViews Bitmap with that. </p>
  
  <p>During the <code>getView</code> function of my customListView class, I create a thread that processes the url from the database and then I parse that, then set that as the ImageView's bitmap. </p>
  
  <p>Here is my <code>getView</code> function:</p>
  
  <pre><code>public View getView(int position, View convertView, ViewGroup parent) {
  
      if (convertView == null) {
  
          convertView = layoutInflater.inflate(R.layout.nominees_list_layout, null);
          holder = new ViewHolder();
  
          holder.firstName = (TextView) convertView.findViewById(R.id.nomineesListFirstName);
          holder.middleName = (TextView) convertView.findViewById(R.id.nomineesListMiddleName);
          holder.lastName = (TextView) convertView.findViewById(R.id.nomineesListLastName);
          holder.photo = (ImageView) convertView.findViewById(R.id.nomineesListCandidatePhoto);
  
  
          convertView.setTag(holder);
      }
      else {
          holder = (ViewHolder) convertView.getTag();
      }
  
      holder.firstName.setText(listData.get(position).getFirst_name());
      holder.middleName.setText(listData.get(position).getMiddle_name());
      holder.lastName.setText(listData.get(position).getLast_name());
  
      new setDisplayPhoto().execute(listData.get(position).getPicture_path());
  
      return convertView;
  }
  </code></pre>
  
  <p>As you can see, I am initializing the view components then setting their values based on the listData taken from the database. The data displayed are all correct. Then, what I do for the imageView is that I create an AsyncTask and feed the data there. </p>
  
  <p>Here is my AsyncTask:</p>
  
  <pre><code>private class setDisplayPhoto extends AsyncTask&lt;String, Void, String&gt; {
      String toastMessage = "Downloading nominees photo finished.";
  
      Bitmap mIcon_val;
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
  
      @Override
      protected String doInBackground(String... params) {
  
          Log.d("","doInBackGround Started");
  
          String photo_url_str  = "http://192.168.0.10:8888/server/path/here/"
                  + params[0];
  
          try {
              Log.d("","photo_url_str = " + photo_url_str);
              newurl = new URL(photo_url_str);
          } catch (MalformedURLException e) {
              e.printStackTrace();
          }
          try {
  
              mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
  
              holder.photo.setImageBitmap(mIcon_val);
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
          return "Executed!";
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
      }
  </code></pre>
  
  <p>However, the photos do not appear, and if they do, sometimes the photos are jumbled up. </p>
  
  <p>Can someone help me with how to properly set the image source of an imageView during the getView function of the customListView Adapter when the source is taken from a local server? Thank you for any help. </p>