博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LayoutInflater 应用

Posted on 2012-08-25 20:52  J_turn  阅读(131)  评论(0)    收藏  举报
 1 public class iconlist extends ListActivity {
 2     
 3     private static class iconAdapter extends BaseAdapter {
 4         private LayoutInflater mInflater;
 5         private Bitmap mIcon1;
 6         private Bitmap mIcon2;
 7         
 8         private static final String[] DATA = {
 9             "Jacob", "Emily", "Michael", "Isabella", 
10             "Ethan", "Emma", "Joshua", "Ava",
11             "Daniel", "Sophia", "Matthew", "Elizabeth"};
12       
13         public iconAdapter(Context context) {
14             mInflater = LayoutInflater.from(context);
15             mIcon1 = BitmapFactory.decodeResource(context.getResources(), 
16                      android.R.drawable.sym_action_call);
17             mIcon2 = BitmapFactory.decodeResource(context.getResources(), 
18                      android.R.drawable.sym_call_incoming);
19         }
20 
21         public int getCount() {
22             return DATA.length;
23         }
24 
25         public Object getItem(int position) {
26             return position;
27         }
28 
29         public long getItemId(int position) {
30             return position;
31         }
32 
33         public View getView(int position, View convertView, ViewGroup parent) {
34             ViewHolder holder;
35 
36             if (convertView == null) {
37                 convertView = mInflater.inflate(R.layout.main, null);
38 
39                 holder = new ViewHolder();
40                 holder.text = (TextView) convertView.findViewById(R.id.text);
41                 holder.icon = (ImageView) convertView.findViewById(R.id.icon);
42 
43                 convertView.setTag(holder);
44             } else {
45                 holder = (ViewHolder) convertView.getTag();
46             }
47 
48             holder.text.setText(DATA[position]);
49             holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
50 
51             return convertView;
52         }
53 
54         static class ViewHolder {
55             TextView text;
56             ImageView icon;
57         }
58     }
59 
60     @Override
61     public void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         setListAdapter(new iconAdapter(this));
64     }
65 }