2

I am getting images from background process by using url.To dispaly images i have used BaseAdapter for display the all images in list.I have added the BaseAdapter to my listView.

I am able to download images at back ground by using AsyncTask class this class can be return bitmap images.

I have implemented MyCustome adapter class as follows.

public class CustomeAdapter extends BaseAdapter {
 private static ArrayList<Message> searchArrayList;

 private LayoutInflater mInflater;

 public CustomeAdapter(Context context, ArrayList<Message> results) {
  searchArrayList = results;
  mInflater = LayoutInflater.from(context);




 }

 public int getCount() {
  return searchArrayList.size();
 }

 public Object getItem(int position) {
  return searchArrayList.get(position);
 }

 public long getItemId(int position) {
  return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.list, null);
   holder = new ViewHolder();
   holder.personName = (TextView) convertView.findViewById(R.id.usrName);
   holder.message = (TextView) convertView.findViewById(R.id.msgText);
   holder.imgPerson = (ImageView) convertView.findViewById(R.id.personImg);
   holder.msgImg = (ImageView) convertView.findViewById(R.id.msgImg);

   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }

  holder.personName.setText(searchArrayList.get(position).getShoutUserFirstName());
  holder.message.setText(searchArrayList.get(position).getShoutMessageText());
  holder.msgImg.setImageResource(R.drawable.smile);

  try{

  new DownloadImageTask( holder.imgPerson).execute("http://www.bwappstore.com/WebApps/ShoutImage/Image.ashx?UserID="+searchArrayList.get(position).getShoutUserID());

  }
  catch (RejectedExecutionException  e) {
    e.printStackTrace();
}
  return convertView;
 }

 static class ViewHolder {
  TextView personName;
  TextView message;
 ImageView imgPerson;
 ImageView msgImg;

 }


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

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }
        protected Bitmap doInBackground(String... urls) {
            Bitmap bm = null;
            try {
                URL aURL = new URL(urls[0]);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
           } 
            catch (IOException e) {
               Log.e("", "Error getting bitmap", e);
           }
           return bm;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
}

}

from the implementation i can download the images and i can show in list view but the images are changing when i scroll those are all not fixing.those are loading again and again and over loading on image but not loading related messages images if one image loaded then it will never load again.after some time only one image is showing in list

how can i load one image for one time then show in list? please any body help me

prasad.gai
  • 2,977
  • 10
  • 58
  • 93

2 Answers2

1

Here's a nice library, which can helps you. The author told that he fought against memory leaks and performance. Look at this library for a start.

andrei_zaitcev
  • 1,358
  • 16
  • 23
1

The problem is in your logic.

You are executing download image tasks in adapter's getView method which gets called every time you scroll the list.

So if your activity is ListActivity, try executing your tasks in onStart method. Then this issue will not be seen.

AndroDev
  • 3,236
  • 8
  • 35
  • 49