Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Enhance performance by using ViewHolder
Browse files Browse the repository at this point in the history
Change-Id: I8199b84b6822944383a79ab8ad3eb227f5e31508
Signed-off-by: Roger Chen <cxr514033970@gmail.com>
  • Loading branch information
cxr514033970 committed Dec 26, 2012
1 parent 04dc066 commit da3bbf3
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/com/android/mms/ui/IconListAdapter.java
Expand Up @@ -35,7 +35,33 @@
public class IconListAdapter extends ArrayAdapter<IconListAdapter.IconListItem> {
protected LayoutInflater mInflater;
private static final int mResource = R.layout.icon_list_item;
private ViewHolder mViewHolder;

static class ViewHolder {
private View mView;
private TextView mTextView;
private ImageView mImageView;

public ViewHolder(View view) {
mView = view;
}

public TextView getTextView() {
if (mTextView == null) {
mTextView = (TextView) mView.findViewById(R.id.text1);
}

return mTextView;
}

public ImageView getImageView() {
if (mImageView == null) {
mImageView = (ImageView) mView.findViewById(R.id.icon);
}

return mImageView;
}
}
public IconListAdapter(Context context,
List<IconListItem> items) {
super(context, mResource, items);
Expand All @@ -44,22 +70,22 @@ public IconListAdapter(Context context,

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
ImageView image;

View view;
if (convertView == null) {
view = mInflater.inflate(mResource, parent, false);
mViewHolder = new ViewHolder(view);
view.setTag(mViewHolder);
} else {
view = convertView;
mViewHolder = (ViewHolder) view.getTag();
}

// Set text field
text = (TextView) view.findViewById(R.id.text1);
TextView text = mViewHolder.getTextView();
text.setText(getItem(position).getTitle());

// Set resource icon
image = (ImageView) view.findViewById(R.id.icon);
ImageView image = mViewHolder.getImageView();
image.setImageResource(getItem(position).getResource());

return view;
Expand Down

0 comments on commit da3bbf3

Please sign in to comment.