Created
April 26, 2012 06:54
-
-
Save JakeWharton/2496926 to your computer and use it in GitHub Desktop.
Template for a list adapter which uses a view holder to cache lookups.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TweetAdapter extends BaseAdapter { | |
// ... | |
public View getView(int position, View convertView, ViewGroup parent) { | |
TweetViewHolder vh = TweetViewHolder.get(convertView, parent); | |
Tweet item = getItem(position); | |
vh.user.setText(item.user); | |
vh.tweet.setText(item.tweet); | |
return vh.root; | |
} | |
} | |
//By making this its own class we allow for reuse in other adapters | |
public class TweetViewHolder { | |
public static TweetViewHolder get(View convertView, ViewGroup parent) { | |
if (convertView == null) { | |
return new TweetViewHolder(parent); | |
} | |
return (TweetViewHolder)convertView.getTag(); | |
} | |
public final View root; | |
public final TextView user; | |
public final TextView tweet; | |
private TweetViewHolder(ViewGroup parent) { | |
root = LayoutInflater.from(parent.getContext()).inflate(R.layout.tweet_view, parent, false); | |
root.setTag(this); | |
user = (TextView)root.findViewById(R.id.user); | |
tweet = (TextView)root.findViewById(R.id.tweet); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok cool just wanted to make sure. Thanks it works great! My lists are iPhone smooth now.