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?