views:

989

answers:

5

Hi, I am a newbie Android guy.

I have a ListView displaying images on every element of the ListView, It works fine... but when I start to scrolling I have realized my image is downloaded again if it is displayed in the screen of my phone device!

How could I stop reloading the images or all the content of the in ListView again? Or how could I avoid reading the getView() function again If I have already downloaded all its content?

+1  A: 

If you are downloading stuff from the internet then you should cache it so that you don't need to download it over and over.

CaseyB
A: 

Could I store in cache the content of my rows in the ListView???

+2  A: 

To expand on CaseyB's suggestion, I'd recommend you watch the Turbo-charge your UI talk

Watch the first section to see if you implementing the adapter properly.

Also start watching from 50:00 for a simple example of a bitmap cache. In fact watch the whole video.

James
A: 

Hey Casey and James thank YOU VERY MUCH for your responses! I fact I used this ...

  static class ViewHolder {
    TextView text;
    ImageView icon;
    }
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);

 convertView.setTag(holder);
 } else {
 holder = (ViewHolder) convertView.getTag();
 }

 holder.text.setText(DATA[position]);
 holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

 return convertView;
 }

from Turbo-Chargeyour UI

but in my case I have to use 3 different views in my ListView! at position 0, at position 5 and another in the other positions...

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

convertView = null; 
//****FORCING TO CREATE EVERYTIME A DIFFERENT VIEW! ( I USE 3 DIFERENT TYPES OF VIEWS)
if (convertView == null) {

if (position == 0) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.iconrowmain, null);
} else if (position == 5) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.iconrowwebadvice, null);          
} else {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.iconrowa, null);
}
}

this makes so slow my app! :( creating everytime a new View :(

If I have already created all my views how could avoid enter again in getView() function when I do the scrolling!????

THANK YOU VERY MUCH! Alexi

P.S. OK JAMES! SIMPLE CACHE!!! :D

A: 

It is possible to create an efficient adapter with different view types. Take a look at this question.

It's fairly simple. Just need to override getItemViewType and getViewTypeCount. Then you can count on convertView being the correct view type.

James