Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2008 Esmertec AG.
      3  * Copyright (C) 2008 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mms.ui;
     19 
     20 import java.util.List;
     21 
     22 import android.content.Context;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.ArrayAdapter;
     27 import android.widget.ImageView;
     28 import android.widget.TextView;
     29 
     30 import com.android.mms.R;
     31 
     32 /**
     33  * An adapter to store icons.
     34  */
     35 public class IconListAdapter extends ArrayAdapter<IconListAdapter.IconListItem> {
     36     protected LayoutInflater mInflater;
     37     private static final int mResource = R.layout.icon_list_item;
     38     private ViewHolder mViewHolder;
     39 
     40     static class ViewHolder {
     41         private View mView;
     42         private TextView mTextView;
     43         private ImageView mImageView;
     44 
     45         public ViewHolder(View view) {
     46             mView = view;
     47         }
     48 
     49         public TextView getTextView() {
     50             if (mTextView == null) {
     51                 mTextView = (TextView) mView.findViewById(R.id.text1);
     52             }
     53 
     54             return mTextView;
     55         }
     56 
     57         public ImageView getImageView() {
     58             if (mImageView == null) {
     59                 mImageView = (ImageView) mView.findViewById(R.id.icon);
     60             }
     61 
     62             return mImageView;
     63         }
     64     }
     65     public IconListAdapter(Context context,
     66             List<IconListItem> items) {
     67         super(context, mResource, items);
     68         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     69     }
     70 
     71     @Override
     72     public View getView(int position, View convertView, ViewGroup parent) {
     73         View view;
     74         if (convertView == null) {
     75             view = mInflater.inflate(mResource, parent, false);
     76             mViewHolder = new ViewHolder(view);
     77             view.setTag(mViewHolder);
     78         } else {
     79             view = convertView;
     80             mViewHolder = (ViewHolder) view.getTag();
     81         }
     82 
     83         // Set text field
     84         TextView text = mViewHolder.getTextView();
     85         text.setText(getItem(position).getTitle());
     86 
     87         // Set resource icon
     88         ImageView image = mViewHolder.getImageView();
     89         image.setImageResource(getItem(position).getResource());
     90 
     91         return view;
     92     }
     93 
     94     public static class IconListItem {
     95         private final String mTitle;
     96         private final int mResource;
     97 
     98         public IconListItem(String title, int resource) {
     99             mResource = resource;
    100             mTitle = title;
    101         }
    102 
    103         public String getTitle() {
    104             return mTitle;
    105         }
    106 
    107         public int getResource() {
    108             return mResource;
    109         }
    110     }
    111 }
    112