-1

I am doing one POC for my project where the employee details i.e. name, position, salary along with his/her image would be displayed in ListView Android.

The complete Employee data is getting fetched from database. 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.
}

Note: Each employee has different image url. PFB code snippet which I am using:

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

onReceive of Braodcast I am setting data to Adapter:

// Other line of code

setListAdapter(new ListEmployeeAdapter());

The ListAdapter is:

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 Download Image Async task:

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

    }
}

Kindly suggest any solution as images are getting setting incorrectly @image view.To be precise the emplyee pic is getting displayed at random location of listview.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Ricky
  • 11
  • 4

2 Answers2

0

Why are you downloading image? use UrlImageviewHelper library. [https://github.com/koush/UrlImageViewHelper][1]

Tabassum Latif
  • 247
  • 2
  • 15
  • Because these images are placed at my server location that needs to be downloaded using DownloadImageTask without any third party library/third person code. – Ricky Sep 09 '15 at 07:37
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);

    }
}
Tabassum Latif
  • 247
  • 2
  • 15