Android中的listview的adapter自定义适配器

写自己的MyAdapter实现BaseAdapter:

public class MyAdapter extends BaseAdapter {

    List<Map<String, Object>> list;
    LayoutInflater inflater;  \\反射,和findviewByid差不多,但还是不同。

    public MyAdapter(Context context){
        inflater = LayoutInflater.from(context);
    }

    public List<Map<String, Object>> getList() {
        return list;
    }

    public void setList(List<Map<String, Object>> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        View view = inflater.inflate(R.layout.item, null); \\通过反射找到item。

        ImageView logo = (ImageView) view.findViewById(R.id.logo);
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView version = (TextView) view.findViewById(R.id.version);
        TextView size = (TextView) view.findViewById(R.id.size);

        Map map = list.get(position);
        logo.setImageResource((Integer) map.get("logo"));
        title.setText((String) map.get("title"));
        version.setText((String) map.get("version"));
        size.setText((String) map.get("size"));

        return view;
    }
}

在mainactivity中实现赋值给listview:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);


        lv_show = (ListView)findViewById(R.id.lv_show);

        //String[] data = {"你","是","一","个","坏", "人", "知", "道", "?"};
        //ArrayAdapter<String> adapters = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

        ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("logo", R.drawable.camera);
        map.put("title", "美颜相机");
        map.put("version", "1.0.1");
        map.put("size", "21.3M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo", R.drawable.music);
        map.put("title", "天天动听");
        map.put("version", "2.1.1");
        map.put("size", "23.1M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo", R.drawable.movie);
        map.put("title", "暴风影音");
        map.put("version", "3.1.0");
        map.put("size", "20.1M");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("logo", R.drawable.google);
        map.put("title", "谷歌浏览");
        map.put("version", "0.1.1");
        map.put("size", "33.1M");
        list.add(map);

        /*SimpleAdapter adapters = new SimpleAdapter(
                this,
                list,
                R.layout.item,
                new String[]{
                        "logo",
                        "title",
                        "version",
                        "size"
                },
                new int[] {
                    R.id.logo, R.id.title, R.id.version, R.id.size
                }

        );*/

        MyAdapter adapters = new MyAdapter(this);
        adapters.setList(list);

        lv_show.setAdapter(adapters);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值