Dhruv Devulapalli
Dhruv Devulapalli

Reputation: 41

Asynctask isn't setting imageresource for an imageview

I'm new to Android, so not really sure what I'm doing wrong here. I have a gridview where I'm displaying a bunch of pictures, and I'm using an adapter to populate the gridview. The gridview was scrolling very slow/laggy, so I tried using asynctask to set the imageresource for the imageviews. For some reason, the imageviews are not getting the resource, so they are just showing up blank. This is my code:

class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            imageView.setLayoutParams(new GridView.LayoutParams(width/2,(width/2)*515/400));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }

        //imageView.setImageResource(mThumbIds[position]);
        ImageSetter task=new ImageSetter(imageView);
        task.execute(position);

        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.zsmall_artemis,R.drawable.zsmall_ash,R.drawable.zsmall_bane,R.drawable.zsmall_belle,R.drawable.zsmall_caleb,R.drawable.zsmall_falcyn,R.drawable.zsmall_jaden,R.drawable.zsmall_julien,R.drawable.zsmall_kody,
            R.drawable.zsmall_mack,R.drawable.zsmall_maxis,R.drawable.zsmall_nero,R.drawable.zsmall_nick,R.drawable.zsmall_nykyrian
    };

    class ImageSetter extends AsyncTask<Integer,Void,Integer>{
        private final WeakReference<ImageView> imageViewReference;

        public ImageSetter(ImageView imageview){
            imageViewReference=new WeakReference<ImageView>(imageview);
        }
        @Override
        protected Integer doInBackground(Integer... ImagePos){
            return mThumbIds[ImagePos[0]];
        }


        protected void onPostExecute(int ImageId){
            if(isCancelled()){
                ImageId=mThumbIds[0];
            }
            if(imageViewReference != null){
                ImageView iview = imageViewReference.get();
                if(iview != null){
                    iview.setImageResource(ImageId);


                }
            }
        }
    }
}

The picture resource ids are all stored in the array, and 'position' is used to indicate which array element I'm referring to. How do I fix this?

Upvotes: 0

Views: 62

Answers (3)

Faruk
Faruk

Reputation: 5851

Try using library for image loader such as Glide. In my opinion, Glide is the best imageLoader ever. It fast, and best at memory management, so you will never get an out of memory exception.

Upvotes: 0

Matt
Matt

Reputation: 33

I don't believe there's any reason to load local assets on another thread. Also when the UI is updated it has to be done on the main thread so multithreading doesn't help with it. Other threads should be used for things like loading images and information from the internet.

Upvotes: 0

Matt
Matt

Reputation: 5704

It might just be that the ImageView or the GridView doesn't realize its been updated. If this is the case, it can be fixed with an invalidate() call.

if(imageViewReference != null){
   ImageView iview = imageViewReference.get();
     if(iview != null){
        iview.setImageResource(ImageId);

        //invalidate call
        iview.invalidate();

   }
}

It may be more than that, though. Let me know if that works for you.

Upvotes: 1

Related Questions