Android适配器之------BaseAdapter(例子)

  1. public class RecentAdapter extends BaseAdapter {
  2.  
  3.     private class RecentViewHolder { 
  4.         TextView appName; 
  5. ImageView appIcon;
  6.         TextView appSize; 
  7.     } 
  8.  
  9.     private List<ResolveInfo> mAppList; 
  10.     private LayoutInflater mInflater; 
  11.     private PackageManager pm; 
  12.  
  13.     public RecentAdapter(Context c, List<ResolveInfo> appList, 
  14.             PackageManager pm) { 
  15.         mAppList = appList; 
  16.         this.pm = pm; 
  17.         mInflater = (LayoutInflater) c 
  18.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  19.     } 
  20.      
  21.     public void clear(){ 
  22.         if(mAppList!=null){ 
  23.             mAppList.clear(); 
  24.         } 
  25.     } 
  26.  
  27.     public int getCount() { 
  28.         return mAppList.size(); 
  29.     } 
  30.  
  31.     @Override 
  32.     public Object getItem(int position) { 
  33.         return mAppList.get(position); 
  34.     } 
  35.  
  36.     @Override 
  37.     public long getItemId(int position) { 
  38.         // TODO Auto-generated method stub 
  39.         return position; 
  40.     } 
  41.  
  42.     public View getView(int position, View convertView, ViewGroup parent) { 
  43.         RecentViewHolder holder; 
  44.         if (convertView == null) { 
  45.             convertView = mInflater.inflate(R.layout.app_info_item, null); 
  46.             holder = new RecentViewHolder(); 
  47.             holder.appName = (TextView) convertView.findViewById(R.id.app_name); 
  48.             holder.appIcon = (ImageView) convertView 
  49.                     .findViewById(R.id.app_icon); 
  50.             holder.appSize = (TextView) convertView.findViewById(R.id.app_size); 
  51.             convertView.setTag(holder); 
  52.         } else
  53.             holder = (RecentViewHolder) convertView.getTag(); 
  54.         } 
  55.         ResolveInfo appInfo = mAppList.get(position); 
  56.         if (appInfo != null) { 
  57.             String labelName = appInfo.loadLabel(pm).toString(); 
  58.             if (labelName != null) { 
  59.                 holder.appName.setText(labelName); 
  60.             } 
  61.  
  62.             Drawable icon = appInfo.loadIcon(pm); 
  63.             if (icon != null) { 
  64.                 holder.appIcon.setImageDrawable(icon); 
  65.             } 
  66.         } 
  67.         return convertView; 
  68.     } 
  69.      
  70.     public void remove(int position){ 
  71.         mAppList.remove(position); 
  72.         this.notifyDataSetChanged(); 
  73.     } 
  74.  

其中两个注意点为:

setTag 用View设置存储数据

notifyDataSetChanged() 告诉View数据更改并刷新

View convertView = mInflater.inflate(R.layout.app_info_item, null)  加载XML Item 示图

app_info_item.xml文件示例

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content" 
  5.     android:layout_gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight"
  6.  
  7.     <ImageView android:id="@+id/app_icon" android:layout_width="@android:dimen/app_icon_size" 
  8.         android:layout_height="@android:dimen/app_icon_size" 
  9.         android:layout_alignParentLeft="true" android:paddingLeft="6dip" 
  10.         android:paddingTop="6dip" android:paddingBottom="6dip" 
  11.         android:scaleType="fitCenter" /> 
  12.  
  13.     <TextView android:id="@+id/app_name" android:layout_width="wrap_content" 
  14.         android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" 
  15.         android:textColor="?android:attr/textColorPrimary" 
  16.         android:layout_toRightOf="@id/app_icon" android:paddingLeft="6dip" 
  17.         android:paddingTop="6dip" /> 
  18.  
  19.     <TextView android:id="@+id/app_description" 
  20.         android:layout_width="wrap_content" android:layout_height="wrap_content" 
  21.         android:textAppearance="?android:attr/textAppearanceSmall" 
  22.         android:layout_below="@+id/app_name" android:layout_toRightOf="@id/app_icon" 
  23.         android:paddingLeft="6dip" android:paddingBottom="6dip" /> 
  24.     <TextView android:id="@+id/app_size" android:layout_width="wrap_content" 
  25.         android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" 
  26.         android:layout_alignParentRight="true" android:layout_below="@+id/app_name" 
  27.         android:paddingRight="6dip" android:maxLines="1" /> 
  28.  
  29. </RelativeLayout> 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值