ListView异步下载图像Android中的图像设置不正确

时间:2015-09-08 17:35:19

标签: android listview android-listview android-asynctask imagedownload

我正在为我的项目做一个POC,其中员工的详细信息,即名称,职位,薪水以及他/她的图像将显示在ListView Android中。

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

x(i)

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

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.
}

onReceive of Braodcast我正在将数据设置为适配器:

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);
}

ListAdapter是:

// Other line of code

setListAdapter(new ListEmployeeAdapter());

PFB下载图像异步任务:

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;
    }

}

请建议任何解决方案,因为图像设置不正确@image view.To准确地说,emplyee pic会在listview的随机位置显示。

1 个答案:

答案 0 :(得分: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);

    }
}