zoukankan      html  css  js  c++  java
  • android ListView之BaseAdapter的使用方式

    通常在使用自己定义适配器的时候,我们都会掌握一种固定的模式。充分利用convertView+缓存的方式。


    	private ArrayList<ListBean> list ;
    	private LayoutInflater mInflater;
    	public DetailListAdapter(Context context,ArrayList<ListBean> list){
    		this.list=list;
    		mInflater = LayoutInflater.from(context);
    	}
    	@Override
    	public int getCount() {
    		// TODO Auto-generated method stub
    		return list == null ?0:list.size();
    	}
    
    	@Override
    	public Object getItem(int position) {
    		// TODO Auto-generated method stub
    		return position;
    	}
    
    	@Override
    	public long getItemId(int position) {
    		return position;
    	}
    
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		ViewHolder holder = null;
    		if(convertView == null){
    			convertView = mInflater.inflate(R.layout.search_info_item_layout, null);
    			holder = new ViewHolder();
    			holder.TimeTxt = (TextView) convertView.findViewById(R.id.time_tv);
    			holder.AddressTxt = (TextView) convertView.findViewById(R.id._address_tv);
    			holder.processFlagTxt = (TextView) convertView.findViewById(R.id.process_flag_tv);
    			convertView.setTag(holder);
    		}else{
    			holder = (ViewHolder) convertView.getTag();
    		}
    		holder.TimeTxt.setText(list.get(position).getTime());
    		holder.AddressTxt.setText(list.get(position).getAddress());
    		holder.processFlagTxt.setText(list.get(position).getState());
    		return convertView;
    	}
    	class ViewHolder{
    		TextView TimeTxt;
    		TextView AddressTxt;
    		TextView processFlagTxt;
    	}
    }
    近期,做百度地图离线下载的时候,发现一种新的方式,非常想知道百度的android程序猿是怎样来写代码的。细致一看,确实有非常大不同。

    来自百度地图离线下载的Demo

    private ArrayList<MKOLUpdateElement> localMapList = null;
    	public class LocalMapAdapter extends BaseAdapter {
    
    		@Override
    		public int getCount() {
    			return localMapList.size();
    		}
    
    		@Override
    		public Object getItem(int index) {
    			return localMapList.get(index);
    		}
    
    		@Override
    		public long getItemId(int index) {
    			return index;
    		}
    
    		@Override
    		public View getView(int index, View view, ViewGroup arg2) {
    			MKOLUpdateElement e = (MKOLUpdateElement) getItem(index);
    			view = View.inflate(OfflineDemo.this,
    					R.layout.offline_localmap_list, null);
    			initViewItem(view, e);
    			return view;
    		}
    
    		void initViewItem(View view, final MKOLUpdateElement e) {
    			Button display = (Button) view.findViewById(R.id.display);
    			Button remove = (Button) view.findViewById(R.id.remove);
    			TextView title = (TextView) view.findViewById(R.id.title);
    			TextView update = (TextView) view.findViewById(R.id.update);
    			TextView ratio = (TextView) view.findViewById(R.id.ratio);
    			ratio.setText(e.ratio + "%");
    			title.setText(e.cityName);
    			if (e.update) {
    				update.setText("可更新");
    			} else {
    				update.setText("最新");
    			}
    			if (e.ratio != 100) {
    				display.setEnabled(false);
    			} else {
    				display.setEnabled(true);
    			}
    			remove.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View arg0) {
    					mOffline.remove(e.cityID);
    					updateView();
    				}
    			});
    			display.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					Intent intent = new Intent();
    					intent.putExtra("x", e.geoPt.longitude);
    					intent.putExtra("y", e.geoPt.latitude);
    					intent.setClass(OfflineDemo.this, BaseMapDemo.class);
    					startActivity(intent);
    				}
    			});
    		}
    
    	}
    



  • 相关阅读:
    yarn/npm 设置镜像地址
    vmware 安装的虚拟机没有网络
    idea + spring-boot 开发时热更新(hotreload)
    @ConfigurationProperties(prefix = "server-options") 抛出 SpringBoot Configuration Annotation Processor not configured 错误
    使用 @ConfigurationProperties 注解 提示 "Spring Boot Configuration Annotation Processor not found in classpath"
    idea 中 spring-boot 项目使用 lombok 编译报错找不到 log
    install docker
    Install Ubuntu on Windows Subsystem for Linux
    解决 js aysnc await try-catch 地狱
    spring-boot rest controller 使用枚举作为参数,重写反序列化实现任意值转枚举类型
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4280255.html
Copyright © 2011-2022 走看看