ListView异步下载图像Android中的图像设置不正确
Posted
技术标签:
【中文标题】ListView异步下载图像Android中的图像设置不正确【英文标题】:Incorrect Image setting in ListView asynchronous download image Android 【发布时间】:2015-12-04 12:02:48 【问题描述】:我正在为我的项目做一个 POC,其中员工详细信息(例如姓名、职位、薪水以及他/她的图像)将显示在 ListView android 中。
正在从数据库中获取完整的员工数据。 PFB员工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 代码 sn-p:
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 我正在向 Adapter 设置数据:
// 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 view。确切地说,员工图片显示在列表视图的随机位置。
【问题讨论】:
有人可以帮忙吗? 【参考方案1】:为什么要下载图片? 使用 UrlImageviewHelper 库。 [https://github.com/koush/UrlImageViewHelper][1]
【讨论】:
因为这些图像放置在我的服务器位置,需要使用 DownloadImageTask 下载,无需任何第三方库/第三方代码。【参考方案2】: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);
【讨论】:
感谢修改代码。我会检查相同的,让你知道。再次感谢!!! 不工作...总是从 doinbackground 返回位图为空以上是关于ListView异步下载图像Android中的图像设置不正确的主要内容,如果未能解决你的问题,请参考以下文章