首页 > 代码库 > 自定义 适配器

自定义 适配器

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

自定义 适配器