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);
}
}