-1

我正在为我的项目做一个POC,其中员工详细信息(即姓名,职位,薪水以及他/她的图像)将显示在ListView Android中。ListView异步下载图像中的图像设置不正确Android

完整的员工数据正从数据库中获取。 PFB Employee BO:

public class Employeeimplements Parcelable, Serializable { 

    @com.google.gson.annotations.SerializedName("id") 
    private String id; 

    @com.google.gson.annotations.SerializedName("name") 
    private String name; 

    @com.google.gson.annotations.SerializedName("position") 
    private String position; 

    @com.google.gson.annotations.SerializedName("displayUrl") 
    private String displayUrl; 

    // Getter, Setter and Parcelable method implementation. 
} 

注意:每个员工都有不同的图片网址。这我使用PFB代码片段:

loadEmplyeeData() { 
    // Lines of code to fetch List of employee from database. 

    // If fetched successful then broadcasting that ready to update view. 

    Intent broadcast = new Intent(); 
    broadcast.setAction("employeedata.loaded"); 
    sendBroadcast(broadcast); 
} 

Braodcast的的onReceive我设置数据适配器:

// Other line of code 

setListAdapter(new ListEmployeeAdapter()); 

的ListAdapter是:

private class ListEmployeeAdapterextends BaseAdapter { 

    List<Employee> empList = this.employees; 

    @Override 
    public int getCount() { 
     return empList .size(); 
    } 

    @Override 
    public Employee getItem(int arg0) { 
     return empList .get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return arg0; 
    } 

    @SuppressLint("InflateParams") 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) ListAidActivity.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.listitem, null); 
      holder = new ViewHolder(); 
      holder.empName = (TextView) convertView.findViewById(R.id.textView1); 
      holder.empPosition = (TextView) convertView.findViewById(R.id.textView2); 
      holder.empName.setTypeface(myTypeface); 
      holder.empPosition.setTypeface(myTypeface); 

      holder.imageView = (ImageView) convertView.findViewById(R.id.lstImage); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Employee emp = (Employee) empList.get(position); 

     holder.empName.setText(emp.getName()); 
     holder.empPosition.setText(emp.getPosition()); 

     if (holder.imageView != null && !CommonUtils.isEmpty(emp.getDisplayUrl())) { 
      new DownloadImageTask((ImageView)holder.imageView).execute(emp.getDisplayUrl()); 
     } 

     return convertView; 
    } 

    public Employee getMyEmplyeeItem(int position) { 
     return empList.get(position); 
    } 

    class ViewHolder { 
     TextView empName; 
     TextView empPosition; 
     ImageView imageView; 
    } 

} 

PFB下载图片异步任务:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 

    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     HttpURLConnection urlConnection = null; 
     Bitmap mIcon11 = null; 
     try { 
      if (urldisplay == null) { 
       return null; 
      } else { 
       URL uri = new URL(urldisplay); 
       urlConnection = (HttpURLConnection) uri.openConnection(); 
       int statusCode = urlConnection.getResponseCode(); 
       if (statusCode != HttpStatus.SC_OK) { 
        return null; 
       } 
       if (urlConnection != null) { 
        InputStream in = urlConnection.getInputStream(); 
        if (in != null) { 
         mIcon11 = BitmapFactory.decodeStream(in); 
         return mIcon11; 
        } else { 
         return null; 
        } 
       } else { 
        return null; 
       } 
      } 

     } catch (Exception e) { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      Log.e("", e.getLocalizedMessage()); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     if (result != null) { 
      bmImage.setImageBitmap(result); 
     } else { 
      Drawable placeholder = bmImage.getContext().getResources().getDrawable(R.drawable.default_img); 
      bmImage.setImageDrawable(placeholder); 
     } 
     bmImage.setScaleType(ScaleType.FIT_XY); 

    } 
} 

请建议任何解决方案作为图像越来越设置不正确@image视图。准确地说,emplyee图片显示在随机位置的listview。

+0

任何人都可以提供帮助吗? – Ricky

回答

0

你为什么要下载图片? 使用UrlImageviewHelper库。 [https://github.com/koush/UrlImageViewHelper][1]

+0

因为这些图像放置在我的服务器位置,需要使用DownloadImageTask进行下载,而无需任何第三方库/第三方代码。 – Ricky

0
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 

    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     HttpURLConnection urlConnection = null; 
     Bitmap mIcon11 = null; 
    Thread thread = new Thread(new Runnable() { 
      @Override 
      public void run() { 
        try { 
      if (urldisplay == null) { 
       return null; 
      } else { 
       URL uri = new URL(urldisplay); 
       urlConnection = (HttpURLConnection) uri.openConnection(); 
       int statusCode = urlConnection.getResponseCode(); 
       if (statusCode != HttpStatus.SC_OK) { 
        return null; 
       } 
       if (urlConnection != null) { 
        InputStream in = urlConnection.getInputStream(); 
        if (in != null) { 
         mIcon11 = BitmapFactory.decodeStream(in); 
         return mIcon11; 
        } else { 
         return null; 
        } 
       } else { 
        return null; 
       } 
      } 

     } catch (Exception e) { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      Log.e("", e.getLocalizedMessage()); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
     } 
      } 
     }); 
     thread.start(); 




     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     if (result != null) { 
      bmImage.setImageBitmap(result); 
     } else { 
      Drawable placeholder = bmImage.getContext().getResources().getDrawable(R.drawable.default_img); 
      bmImage.setImageDrawable(placeholder); 
     } 
     bmImage.setScaleType(ScaleType.FIT_XY); 

    } 
} 
+0

感谢您的修改代码。我会检查一下并让你知道。再次感谢!!! – Ricky

+0

not workin ...返回位图为null始终从doinbackground – Ricky