zoukankan      html  css  js  c++  java
  • Listview之优化BaseAdapter中的getView中的contentView

    BaseAdapter中getView中改动的地方:

    @Override
        public View getView(int position, View contentView, ViewGroup arg2) {
            TextView textview;
            ImageView imageView;
            //判断contentView是否为空,为空重新创建
            if(contentView == null){
                contentView =layoutInflater.inflate(R.layout.list_item, null);
                 textview=(TextView)contentView.findViewById(R.id.textview);
                 textview.setText((CharSequence) list.get(position).get("name"));
                 imageView=(ImageView)contentView.findViewById(R.id.imageview);
                 imageView.setImageResource((Integer) list.get(position).get("pictrue"));
            }else{
                //contentView不为空,返回已经存在的contentView
                return contentView;
            }
            


    获取数据源中改动的地方:

    MainActivity.java中的getData();

    private List<Map<String, Object>> getData() {
            // TODO Auto-generated method stub
    
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", "图1");
            map.put("pictrue", R.drawable.tu1);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("name", "图2");
            map.put("pictrue", R.drawable.tu2);
            list.add(map);
            
            
            map = new HashMap<String, Object>();
            map.put("name", "图3");
            map.put("pictrue", R.drawable.tu3);
            list.add(map);
            
            
            map = new HashMap<String, Object>();
            map.put("name", "图4");
            map.put("pictrue", R.drawable.tu4);
            list.add(map);
            
            map = new HashMap<String, Object>();
            map.put("name", "图5");
            map.put("pictrue", R.drawable.tu5);
            list.add(map);
            
            
            map = new HashMap<String, Object>();
            map.put("name", "图6");
            map.put("pictrue", R.drawable.tu6);
            list.add(map);
            
            map = new HashMap<String, Object>();
            map.put("name", "图7");
            map.put("pictrue", R.drawable.tu7);
            list.add(map);
            
            map = new HashMap<String, Object>();
            map.put("name", "图8");
            map.put("pictrue", R.drawable.tu8);
            list.add(map);
            
            return list;
        }
  • 相关阅读:
    js三种经典排序:冒泡排序、插入排序、快速排序
    CSS小技巧
    2017
    实际开发中的问题积累【积累】
    F.I.S本地环境的搭建教程
    移动端前端开发注意点(未完待续)
    【六】PHP正则表达式方法
    【五】PHP数组操作函数
    【三】php之梗
    【二】php常用方法
  • 原文地址:https://www.cnblogs.com/childhooding/p/4325014.html
Copyright © 2011-2022 走看看