首页 > 代码库 > 适配器的经典写法
适配器的经典写法
class GridViewAdapter extends BaseAdapter{ LayoutInflater inflater; List<PackageInfo> pkInfos; public GridViewAdapter(Context context,List<PackageInfo> packageInfos) { inflater = LayoutInflater.from(context); this.pkInfos = packageInfos; } @Override public int getCount() { // TODO Auto-generated method stub return pkInfos.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return pkInfos.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.gv_item, null); TextView tv = (TextView)view.findViewById(R.id.gv_item_appname); ImageView iv = (ImageView)view.findViewById(R.id.gv_item_icon); tv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager())); iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager())); return view; } }
1 class ListViewAdapter extends BaseAdapter{ 2 3 LayoutInflater inflater; 4 List<PackageInfo> pkInfos; 5 public ListViewAdapter(Context context,List<PackageInfo> packageInfos) { 6 inflater = LayoutInflater.from(context); 7 this.pkInfos = packageInfos; 8 } 9 @Override10 public int getCount() {11 // TODO Auto-generated method stub12 return pkInfos.size();13 }14 15 @Override16 public Object getItem(int position) {17 // TODO Auto-generated method stub18 return pkInfos.get(position);19 }20 21 @Override22 public long getItemId(int position) {23 // TODO Auto-generated method stub24 return position;25 }26 27 @Override28 public View getView(int position, View convertView, ViewGroup parent) {29 // TODO Auto-generated method stub30 31 View view = inflater.inflate(R.layout.lv_item, null);32 TextView ntv = (TextView)view.findViewById(R.id.lv_item_appname);33 TextView ptv = (TextView)view.findViewById(R.id.lv_item_packagename);34 ImageView iv = (ImageView)view.findViewById(R.id.lv_icon);35 ntv.setText(pkInfos.get(position).packageName);36 ptv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));37 iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));38 39 return view;40 }41 42 }
尽量自己构建适配器,这样加载速度快,效率高。哪怕简单的也尽量不要使用SimpleAdapter
适配器的经典写法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。