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 implementing more fetch data when list arrive to end only works once
How to fix background red color TextInputLayout when isEmpty in Android
Show video preview after capturing audio
How to use GridView scrollListBy method in Jelly Bean?
Cognito User Pool: How to refresh Access Token Android
What is the usage of “KEY_LEVEL” in Android MediaFormat.setInteger()?
How do you change the nav-bar background (not just color), even when not in foreground?
How to open particular fragment on the click of the push notification message?
Why Window Outlook Rest API does not support Android
Android Navigation Drawer Menu Section without Title [duplicate]
Prevent views in relative layout from overlapping in spinner
Filter data on firebase with Searchview
Assign width to elements according to screen size in Android
Last byte[] only send to the server by using MultipartEntity in android
How to change Android Button ripple colors while preserving other style properties?
Parted error: Unable to satisfy all constraints on the partitions in Android

Categories

HOME
django
beautifulsoup
gridview
delphi-7
amp-html
static
pascal
composite-primary-key
gradient
pdfbox
permissions
dojo
3d-reconstruction
hugo
bookmarks
tvos
nfs
pkcs#11
performancepoint
dkim
ycsb
datastage
atlassian
easyphp
template-engine
asp.net-mvc-viewmodel
boost-icl
poedit
string-concatenation
moses
android-sharedpreferences
flooding
ms-access-2003
inversion-of-control
datastax-enterprise-graph
errbot
user-defined-functions
xbox
scatter3d
gojs
activexobject
hibernate-search
wurfl
graphhopper
tasm
portforwarding
escpos
preloader
logback-groovy
importerror
puredata
httrack
libgphoto2
mschart
geotiff
tinymce-3
static-cast
arules
payload
seq
type-theory
slackware
usart
docpad
atlas
meteor-packages
embedding
stream-processing
riot
nio2
android-launcher
visual-sourcesafe-2005
canalyzer
mod-proxy
wso2greg
sql-server-data-tools
omnifaces
mraid
bufferedinputstream
angular-gettext
punycode
insight.database
build-script
template-deduction
ios8-extension
dot42
decoupling
websphere-esb
drupal-nodes
xmltodict
bin
cascadingdropdown
jquery-blockui
qtconcurrent
os.system
subproject
w3c-geolocation
massive
twitterizer
office-2007
message-passing
isapi-extension
linfu-dynamicproxy
java1.4
dsoframer
lemmatization
xslcompiledtransform
wcf-proxy

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