0

Aswered by @daniel Nugget: Usually for this type of thing you would use Picasso or Glide.... See here for example: https://futurestud.io/blog/picasso-adapter-use-for-listview-gridview-etc

This is something that has been bothering for some time now. In a simple situation you would use an adapter to convert an array of data into a list/grid. However in my case I am downloading the images to show from my database. So while I was looking for an answer why some items were duplicated, I came accros this post here:

Items inside GridView getting repeated when screen scrolls

With the following code:

public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(     Context.LAYOUT_INFLATER_SERVICE );
        v = inflater.inflate(R.layout.gridview_item_layout, parent, false);
    } else {
        v = (View) convertView;
    }
    TextView text = (TextView)v.findViewById(R.id.grid_item_text);
    text.setText(mTextIds[position]);
    ImageView image = (ImageView)v.findViewById(R.id.grid_item_image);
    image.setImageDrawable(mThumbIds[position]);
    return v;
}

Now if my understandings are correct, this code works as following:

1) if the currently being checked view has no value, we have to intantiate it as a new one. So we use an inflator to assign a layout to it, and add it to a viewGroup parent (the grid/listview).

2) if the currently being checked view has a value, we simply return it as it is when it was being created the previous time.

3) in both cases (a new or existing View) we are going to reassign all the values.

What I dont get, is why all values have to be reassigned every singe time, as this even keeps happening when I dont scroll trough my Grid/ListView. The problem I have with it is that it seems that again, even if I dont scroll, my DownloadImageTask (custom class) is being called over and over again, and this will cost the user internet data. Not something that is great for people with limited data.

So to sum up my 3 main questions:

1) why do we have to reassign the values every single time, even if the view was already created previously in order to prevent duplicated items?

2) why does my log show me that my Adapter (and thus re-downloading my images) is doing something, even if I dont scroll?

3) can I adjust the code in such a way that my images are only being downloaded once, and only for those items that have been showed (so we dont have to download (in the long run) 500 images at once, and still "save" where what image belongs?

Community
  • 1
  • 1
sdieters
  • 175
  • 13
  • 1
    Usually for this type of thing you would use Picasso or Glide.... See here for example: https://futurestud.io/blog/picasso-adapter-use-for-listview-gridview-etc – Daniel Nugent Jul 10 '16 at 14:53
  • Thanks, I will check it out! – sdieters Jul 10 '16 at 14:58
  • @DanielNugent you are a life saver. It took me a bit to get it wrking the way I wanted, but it works like a charm now. How can I make your comment as the accepted answer? – sdieters Jul 11 '16 at 14:33

1 Answers1

1

1) Because you are reusing the View. You assign a new values to an object, so Android saves machine resources instead of unloading and loading object to memory. This will not happen, unless you scroll, if it does - you are doing something wrong.

2) Because you are doing something wrong, I suppose you are updating the whole adapter each time one new image is finishes loading, if you will post more code - it will be easier to say.

3) Of course, there are lot of techniques, I suggest you start learning about Cache and dealing with large bitmaps.

Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81
  • Thanks for your reply! That makes sense, however I dont know what I should have done wrong then. I have added my code to the original post. Mind to check it out? I also just came up with an idea where I will add the downloaded images to a bitmap array first, and then assign the bitmaps to the objects. This should give me a bit more controll as I can check if said position in the array isnt occupied yet. I havent tried it yet, but I will definitly check out your links! As for the bitmaps themselves, I made sure that these are thumbnails that are not bigger then 150x150 pixels. – sdieters Jul 10 '16 at 14:38