android


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

Related Links

Android TextView `ifAtLineStart` method?
play two audio files simultaneously
Intent does not resolve
tabview with fragments in android
Custom android.widget.Filter
SQLiteDatabase warning Android
Android:How to apply theme to android application
binding to running Service (after finish() ) / callback Handler
Android, ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”!
How to route audio through bluetooth headphones on Android 4.0.3 rooted tablet
Run full screen on HTC Touch HD2 with Android 2.3
how to rotate the android widget text? and something I don't understand
Change background color on Android Listview Item in non touch mode
WebSockets for Android in Phonegap application with Node.js/Socket.IO server&client
Android Service error to ui
Errors playing YouTube content from within an Android Webview on Google TV

Categories

HOME
microsoftgraph
porting
fuelux
string
powerpoint
jwplayer
nuget
javacc
square
title
ibeacon-android
gson
line
ipmitool
google-form
comsol
plink
dynamic-sql
intershop
chargify
jersey-1.0
google-picker
jmp
nim
database-connection
squashfs
visual-studio-extensions
exit
zxing
interpreter
draft-js-plugins
onload
licensing
spotipy
subclass
user-defined-functions
winpe
magento-1.8
google-cast
send
mobx
volatile
anki
weather
left-join
android-sugarorm
nested-lists
slacktextviewcontroller
alphabetical
jpad
xdocreport
demo
unordered-map
resolution
dcmtk
activeperl
mongotemplate
openblas
carrot2
qtranslate
headless
ngmaterial
reshape
offline.js
vibrate
django-1.10
cubism.js
pytables
preprocessor
centrifuge
macaulay2
xdoclet
udpclient
refinerycms
http-status-code-401
omniauth-facebook
yajsw
beanstalk
piracy-prevention
spring-retry
vigenere
rate-limiting
doctype
rpart
matlab-deployment
node-serialport
osc
rapidsvn
browserify-shim
paw
cbind
live555
ray
dot42
android-audiorecord
mysql-error-1044
inputmismatchexception
hidapi
script#
patricia-trie
os.system
faye
landscape-portrait
clrstoredprocedure
tmx
diagrams
application-verifier
qtabbar
mathematical-notation
google-wave
interop-domino
perl5.12
product-management
dsoframer
xslcompiledtransform
movieplayer
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App