0

I am using the recyclerview to list the image without using third party.I fetch the image from internet.I face the problem that before loading the image on the particular position,i got other image on the particular position for some times after that i got a original image while scrolling.

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

  final ImageDetail current=data.get(position);

   // holder.image.setImageResource(current.imageid);.
     Drawable place=holder.image.getContext().getResources().getDrawable(R.drawable.ic_place);
     holder.image.setImageDrawable(place);
     String url=current.imageuri;
     //   notifyItemInserted(position);
     new ShowImage(holder.image).execute(url);


}


enter code here */
public class ShowImage extends AsyncTask<String,Void,Bitmap>{
    private  WeakReference<ImageView> imageview;
    public ShowImage(ImageView imv){
        imageview=new WeakReference<ImageView>(imv);
    }
    /* Background process
     * input:url
     * output: Bitmap image
     * It passed into onPostExecute method
     *
    */
    @Override
    protected Bitmap doInBackground(String... urls) {

       return getBitMapFromUrl(urls[0]);

    }
    /* This method called after the doINputBackground method
     * input:Bitmap image
     * output: image set into the image view
     * Image view  passed from RecyclerViewOperation to ShowImage class through constructor
     *
    */
    @Override
    protected void onPostExecute(Bitmap result) {
        if((imageview!=null)&&(result!=null)){
            ImageView imgview=imageview.get();
             if(imgview!=null){

                 imgview.setImageBitmap(result);

             }
        }
    }
    /* This method called by doInBackground method
     * input:url
     * output: Bitmap image
     *
    */
    private Bitmap getBitMapFromUrl( String imageuri){
        HttpURLConnection connection=null;

        try {
            URL url=new URL(imageuri);
            connection= (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream is=connection.getInputStream();
            Bitmap mybitmap=BitmapFactory.decodeStream(is);
            return mybitmap;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(connection!=null) {
                connection.disconnect();
            }
        }
    }

}
rafeek
  • 103
  • 1
  • 4
  • 13
  • post your code. How do you expect us to help you without any code? – Akash Singh Oct 31 '15 at 05:36
  • If you are going with your own implementation, then make a `Queue` for processing download request of images. – Krrishnaaaa Oct 31 '15 at 05:39
  • please put complete code. and please explain ** new ShowImage(holder.image).execute(url);** is this related to any library (volley or any other image loading library). – Shubham AgaRwal Oct 31 '15 at 07:28
  • @Krrishnaaaa how can i implemented the queue concept in my code.could u tell me.... – rafeek Nov 02 '15 at 06:34
  • I got the solution.[Multi threading concept](http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html) – rafeek Nov 04 '15 at 06:35

1 Answers1

0

Yes. It's normal. If you're already familiar with ListView's recycling mechanism, you could now guess the why.

In your case, the best solution I've ever seen would be using a rich 3rd party library. is the best, IMO. Home Page.

One of its lovely features is handling ListViews. So, you can simply rely on picasso to handle such issues.

If you still haven't been conviced, kindly let me know to post an answer without any 3rd parties.

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129