国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中BaseAdapter用法示例

2019-12-12 05:37:35
字體:
來源:轉載
供稿:網友

本文實例講述了Android中BaseAdapter用法。分享給大家供大家參考,具體如下:

概述:

BaseAdapter就Android應用程序中經常用到的基礎數據適配器,它的主要用途是將一組數據傳到像ListView、Spinner、Gallery及GridView等UI顯示組件,它是繼承自接口類Adapter

BaseAdapter

Java代碼:

public class RecentAdapter extends BaseAdapter {  private class RecentViewHolder {    TextView appName;    ImageView appIcon;    TextView appSize;  }  private List<ResolveInfo> mAppList;  private LayoutInflater mInflater;  private PackageManager pm;  public RecentAdapter(Context c, List<ResolveInfo> appList,      PackageManager pm) {    mAppList = appList;    this.pm = pm;    mInflater = (LayoutInflater) c        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  }  public void clear(){    if(mAppList!=null){      mAppList.clear();    }  }  public int getCount() {    return mAppList.size();  }  @Override  public Object getItem(int position) {    return mAppList.get(position);  }  @Override  public long getItemId(int position) {    // TODO Auto-generated method stub    return position;  }  public View getView(int position, View convertView, ViewGroup parent) {    RecentViewHolder holder;    if (convertView == null) {      convertView = mInflater.inflate(R.layout.app_info_item, null);      holder = new RecentViewHolder();      holder.appName = (TextView) convertView.findViewById(R.id.app_name);      holder.appIcon = (ImageView) convertView          .findViewById(R.id.app_icon);      holder.appSize = (TextView) convertView.findViewById(R.id.app_size);      convertView.setTag(holder);    } else {      holder = (RecentViewHolder) convertView.getTag();    }    ResolveInfo appInfo = mAppList.get(position);    if (appInfo != null) {      String labelName = appInfo.loadLabel(pm).toString();      if (labelName != null) {        holder.appName.setText(labelName);      }      Drawable icon = appInfo.loadIcon(pm);      if (icon != null) {        holder.appIcon.setImageDrawable(icon);      }    }    return convertView;  }  public void remove(int position){    mAppList.remove(position);    this.notifyDataSetChanged();  }}

其中兩個注意點為:

setTag 用View設置存儲數據

notifyDataSetChanged() 告訴View數據更改并刷新

View convertView = mInflater.inflate(R.layout.app_info_item, null)  加載XML Item 視圖

app_info_item.xml文件示例:

xml代碼:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent" android:layout_height="wrap_content"  android:layout_gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight">  <ImageView android:id="@+id/app_icon" android:layout_width="@android:dimen/app_icon_size"    android:layout_height="@android:dimen/app_icon_size"    android:layout_alignParentLeft="true" android:paddingLeft="6dip"    android:paddingTop="6dip" android:paddingBottom="6dip"    android:scaleType="fitCenter" />  <TextView android:id="@+id/app_name" android:layout_width="wrap_content"    android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"    android:textColor="?android:attr/textColorPrimary"    android:layout_toRightOf="@id/app_icon" android:paddingLeft="6dip"    android:paddingTop="6dip" />  <TextView android:id="@+id/app_description"    android:layout_width="wrap_content" android:layout_height="wrap_content"    android:textAppearance="?android:attr/textAppearanceSmall"    android:layout_below="@+id/app_name" android:layout_toRightOf="@id/app_icon"    android:paddingLeft="6dip" android:paddingBottom="6dip" />  <TextView android:id="@+id/app_size" android:layout_width="wrap_content"    android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall"    android:layout_alignParentRight="true" android:layout_below="@+id/app_name"    android:paddingRight="6dip" android:maxLines="1" /></RelativeLayout>

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android視圖View技巧總結》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资溪县| 凌云县| 文山县| 东港市| 苍山县| 临洮县| 景洪市| 大姚县| 昌平区| 临潭县| 吴旗县| 昌邑市| 拉萨市| 钦州市| 谷城县| 宜兰县| 吉木萨尔县| 怀柔区| 甘南县| 庆云县| 清徐县| 克拉玛依市| 米泉市| 宜君县| 双江| 肇庆市| 甘泉县| 灵丘县| 榆林市| 苏尼特右旗| 大港区| 道孚县| 潞城市| 台中市| 金乡县| 即墨市| 股票| 讷河市| 南川市| 邛崃市| 隆化县|