Android BaseAdapter 例子

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值