ListView

 ArrayAdapter:

        //准备数据
        String data[]={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A"};
        //准备ArrayAdapter对象
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,R.layout.list_array_adapter,data);
        //设置适配器
        lv_main.setAdapter(adapter);

SimpleAdapter:

        List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
        Map<String,Object> map =new HashMap<>();
        map.put("icon",R.drawable.a1);
        map.put("tv_name","name1");
        map.put("tv_content","content1");
        data.add(map);

        map =new HashMap<>();
        map.put("icon",R.drawable.a2);
        map.put("tv_name","name2");
        map.put("tv_content","content2");
        data.add(map);

        map =new HashMap<>();
        map.put("icon",R.drawable.a3);
        map.put("tv_name","name3");
        map.put("tv_content","content3");
        data.add(map);

        map =new HashMap<>();
        map.put("icon",R.drawable.a4);
        map.put("tv_name","name4");
        map.put("tv_content","content4");
        data.add(map);

        map =new HashMap<>();
        map.put("icon",R.drawable.a5);
        map.put("tv_name","name5");
        map.put("tv_content","content5");
        data.add(map);

        map =new HashMap<>();
        map.put("icon",R.drawable.a6);
        map.put("tv_name","name6");
        map.put("tv_content","content6");
        data.add(map);

        map =new HashMap<>();
        map.put("icon",R.drawable.a7);
        map.put("tv_name","name7");
        map.put("tv_content","content7");
        data.add(map);

        String from []={"icon","tv_name","tv_content"};
        int to[]={R.id.icon,R.id.tv_name,R.id.tv_content};

        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.list_simple_adapter,from,to);
        lv_main.setAdapter(adapter);

BaseAdapter:

        List<ShopInfo> data = new ArrayList<ShopInfo>();
        data.add(new ShopInfo(R.drawable.a1,"name1","content1"));
        data.add(new ShopInfo(R.drawable.a2,"name2","content2"));
        data.add(new ShopInfo(R.drawable.a3,"name3","content3"));
        data.add(new ShopInfo(R.drawable.a4,"name4","content4"));
        data.add(new ShopInfo(R.drawable.a5,"name5","content5"));
        data.add(new ShopInfo(R.drawable.a6,"name6","content6"));
        data.add(new ShopInfo(R.drawable.a7,"name7","content7"));

        lv_main.setAdapter(new Myadapter());
    class Myadapter extends BaseAdapter{

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView = View.inflate(MainActivity.this, R.layout.list_simple_adapter, null);
            }
            ShopInfo shopInfo = data.get(position);
            ImageView imageView = convertView.findViewById(R.id.icon);
            imageView.setBackgroundResource(shopInfo.getIcon());
            TextView name = convertView.findViewById(R.id.tv_name);
            TextView content  = convertView.findViewById(R.id.tv_content);
            name.setText(shopInfo.getName());
            content.setText(shopInfo.getContent());

            return convertView;

        }
    }

提高效率:

        ViewHolder viewHolder;
            if(convertView==null){
        convertView = View.inflate(MainActivity.this, R.layout.list_simple_adapter, null);
        viewHolder  =new ViewHolder();
        viewHolder.imageView=(ImageView) convertView.findViewById(R.id.icon);
        viewHolder.name=(TextView) convertView.findViewById(R.id.tv_name);
        viewHolder.content=(TextView) convertView.findViewById(R.id.tv_content);
        convertView.setTag(viewHolder);
    }else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
            ShopInfo shopInfo=data.get(position);
            viewHolder.imageView.setBackgroundResource(shopInfo.getIcon());
            viewHolder.name.setText(shopInfo.getName());
            viewHolder.content.setText(shopInfo.getContent());

            return convertView;
    class ViewHolder{
        ImageView imageView;
        TextView name;
        TextView content;
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值