码迷,mamicode.com
首页 > 其他好文 > 详细

自定义 适配器

时间:2015-02-02 07:13:43      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:private   适配器   public   

public class MainActivity extends Activity {

 

          private ListView lv;

          private String[] objects = new String[] { "潘洋", "波姐", "阳姐", "达兄" };

          private int[] imgs = new int[] { R.drawable.logo, R.drawable.logo,

                              R.drawable.logo, R.drawable.logo };

 

          @Override

          protected void onCreate(Bundle savedInstanceState) {

                    super.onCreate(savedInstanceState);

                    setContentView(R.layout.activity_main);

                    // 1,找到控件

                    lv = (ListView) findViewById(R.id.lv_list);

 

                    // 2,定义适配器

                    // >>>>>>>>>>>>>>>>>>>2.3自定义适配器

                    //MyAdapter adapter = new MyAdapter();

                    // 3,加载适配器

                    lv.setAdapter(adapter);

          }

 

          private class MyAdapter extends BaseAdapter {

 

                    @Override

                    public int getCount() {

                              // TODO Auto-generated method stub

                              return objects.length;

                    }

 

                    @Override

                    public View getView(int position, View convertView, ViewGroup parent) {

                              // 加载布局 activity加载布局

                              /*

                               * LayoutInflater inflater=getLayoutInflater();

                               * inflater.inflate(resource, root);

                               */

                              View contentView = View.inflate(getApplicationContext(),

                                                  R.layout.list_item, null);

                              // 找到布局里面的控件

 

                              ImageView iv = (ImageView) contentView.findViewById(R.id.iv_logo);

                              TextView tv = (TextView) contentView.findViewById(R.id.tv_name);

 

                              // 加载相应数据

                              iv.setImageResource(imgs[position]);

                              tv.setText(objects[position]);

                              return contentView;

                    }

                    @Override

                    public Object getItem(int position) {

                              // TODO Auto-generated method stub

                              return null;

                    }

                    @Override

                    public long getItemId(int position) {

                              // TODO Auto-generated method stub

                              return 0;

                    }

          }

}

 

 

今天自己按照教材,做了个自定义适配器,控件那部分还是不怎么搞得清楚

本文出自 “xmy” 博客,请务必保留此出处http://88649164.blog.51cto.com/9890093/1610502

自定义 适配器

标签:private   适配器   public   

原文地址:http://88649164.blog.51cto.com/9890093/1610502

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!