Asmaa Rashad
Asmaa Rashad

Reputation: 603

Android-load images from url into listViiew occurs every time scrolling the view

I have a list view and each row is about Text and image loaded from url.

on first load of the View first 3 rows are rendered and filled with text and take few seconds to load the image in another thread.

when i am scrolling down new rows appears without images also and load it ,but when i am scrolling up i find that the images uploaded before has gone ,the text only is exist and reload images again.

I want to load images one time and when i am scrolling up and down images not reloaded again once it loaded one time.

this is my code :

 public class MainActivity extends AppCompatActivity {
        ListViewCountries listViewCountries =new ListViewCountries();
        @Override
         protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                listViewCountries.FillCountriesList();


  setContentView(R.layout.activity_main);
            CountryAdapter adapter=new CountryAdapter();
            ListView lst=(ListView)findViewById(R.id.listView);
            lst.setAdapter(adapter);
        }
        class  CountryAdapter extends BaseAdapter {

            @Override
            public int getCount() {
                return listViewCountries.Count;
            }

            @Override
            public Object getItem(int position) {
                return listViewCountries.Countries.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                    List<Country> items = listViewCountries.Countries;
                    LayoutInflater inflater = getLayoutInflater();
                    View rowView = inflater.inflate(R.layout.row_cell, null);
                    ImageView img = (ImageView) rowView.findViewById(R.id.flagImg);
                    new DownloadImageTask(img).execute(items.get(position).FlagURL);
                    TextView name = (TextView) rowView.findViewById(R.id.countryName);
                    TextView description = (TextView) rowView.findViewById(R.id.shortText);
                    name.setText(items.get(position).Name);
                    description.setText(items.get(position).ShortDescription);
                    return rowView;


            }
        }
        private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
            ImageView bmImage;

            public DownloadImageTask(ImageView bmImage) {
                this.bmImage = bmImage;
            }

            protected Bitmap doInBackground(String... urls) {

                String urldisplay = urls[0];
                Bitmap mIcon11 = null;
                try {
                    InputStream in = new java.net.URL(urldisplay).openStream();
                    mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
            }

            protected void onPostExecute(Bitmap result) {
                this.bmImage.setImageBitmap(result);
            }
        }

Upvotes: 2

Views: 1309

Answers (1)

varunkr
varunkr

Reputation: 5552

This is pretty obvious from your code. Inside getView you are calling

            new DownloadImageTask(img).execute(items.get(position).FlagURL);

As you scroll getView gets called for each item and thus images are loaded again, because in getView you fetch image from url, there is no cache mechanism. What you need to do is to cache copies of the images, that way it won't download the image again and again and simply display the file that is present in the cache. You can try to build a cache manually like this.

Or better for you you can use picasso. Its pretty easy to use, just follow the documentation.

Upvotes: 2

Related Questions