Skip to content Skip to sidebar Skip to footer

Image Reloaded When Scroll Listview In Android

I am trying write a news list application. I had parsed JSON data from Server and created CustomListAdapter. There are three textView and imageView in my listView. It works great,

Solution 1:

Try Below code, It might help you to resolve your problem.

publicclassListViewCustomAdapterextends..{

   //Map to cache Image Bitmap. Key= imageUrl,value =Image BitmapprivateMap<String, Bitmap> mBitmapCache = newHashMap<String, Bitmap>();    

   @OverridepublicViewgetView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;

      if (convertView == null) {
          convertView = inflater.inflate(R.layout.row, null);
          holder = newViewHolder();
          holder.nTitle = (TextView) convertView.findViewById(R.id.listTitle);
          holder.nDate = (TextView) convertView.findViewById(R.id.listDate);
          holder.nCategory = (TextView) convertView.findViewById(R.id.listCategory);
          holder.nImage = (ImageView) convertView.findViewById(R.id.listImage);
          convertView.setTag(holder);
      } else {
          holder = (ViewHolder) convertView.getTag();
      } 

      if (newsList.get(position) != null) {
          holder.imageUrl = newsList.get(position).image;
          holder.nTitle.setText(newsList.get(position).title);
          holder.nCategory.setText(newsList.get(position).category);
          holder.nDate.setText(Base.getInstance(context).getDateString(newsList.get(position).date));
          if (holder.imageUrl != null && !holder.imageUrl.equals("null")) {
               String thumbUrl = holder.imageUrl.substring(0, holder.imageUrl.lastIndexOf('.')) + "-260x145" + holder.imageUrl.substring(holder.imageUrl.lastIndexOf('.'), holder.imageUrl.length());
               holder.setImage(thumbUrl);
          }
      }
      return convertView;
   }

   privatestaticclassViewHolder {
        TextView nTitle;
        TextView nDate;
        TextView nCategory;
        ImageView nImage;
        String imageUrl;

        publicvoidsetImage(String imageUrl) {
           this.imageUrl = imageUrl;
           Bitmap imageBitmap = mBitmapCache.get(imageUrl);
           if(imageBitmap!=null){
               nImage.setImageBitmap(imageBitmap);
           } else {
               AsyncHttpClient client = newAsyncHttpClient();
               client.get(imageUrl, null, fileHandler);
           }
        }

        FileAsyncHttpResponseHandler fileHandler = newFileAsyncHttpResponseHandler(context) {
             @OverridepublicvoidonFailure(int statusCode, Header[] headers, Throwable throwable,
                      File response) {
             }

             @OverridepublicvoidonSuccess(int statusCode, Header[] headers, File response) {
                   Bitmap imageBitmap = BitmapFactory.decodeFile(response.getPath());
                   imageView.setImageBitmap(imageBitmap);
                   mBitmapCache.put(imageUrl, imageBitmap);         
             }
        };
    }
}

Solution 2:

Your code in

publicvoidsetImage() {
    if (imageUrl != null && !imageUrl.equals("null") && !imageUrl.equals("")) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(imageUrl, null, fileHandler);
    }
}

looks suspicious: any valid URL will force a reloading as it matches all three conditions in

    imageUrl != null && !imageUrl.equals("null") && !imageUrl.equals("")

Example: http://www.google.com/logo.png is not null, does not equal the string "null" nor the string "", so the if will always be true and the AsyncLoader created. BTW, checking for "null" only makes sense if you are explicitly using that somewhere else in your code. Most probably you only want to check for the null value, not the "null" string.

Post a Comment for "Image Reloaded When Scroll Listview In Android"