PowenKo, Android, Tutorial 032-5, ListView, slow way, good way, fast way,

August 22, 2011   ·   0 Comments

slow way


public View getView(int position, View convertView, ViewGroup parent) { 
     View item = mInflater.inflate(R.layout.list_item_icon_text, null);
     ((TextView) item.findViewById(R.id.text)).setText(DATA[position]); 
     ((ImageView) item.findViewById(R.id.icon)).setImageBitmap( 
             (position & 1) == 1 ? mIcon1 : mIcon2);
     return item; 
 }

right way

public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
         convertView = mInflater.inflate(R.layout.item, parent, false); 
     } 
     ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]); 
     ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap( 
             (position & 1) == 1 ? mIcon1 : mIcon2); 
     return convertView; 
 }

fast way

 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, 
                    parent, false);
            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; 
    }
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon