Android中BaseAdapter用法示例
本文實(shí)例講述了Android中BaseAdapter用法。分享給大家供大家參考,具體如下:
概述:
BaseAdapter就Android應(yīng)用程序中經(jīng)常用到的基礎(chǔ)數(shù)據(jù)適配器,它的主要用途是將一組數(shù)據(jù)傳到像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();
}
}
其中兩個(gè)注意點(diǎn)為:
setTag 用View設(shè)置存儲(chǔ)數(shù)據(jù)
notifyDataSetChanged() 告訴View數(shù)據(jù)更改并刷新
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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android自定義viewgroup 使用adapter適配數(shù)據(jù)(6)
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
- Android自定義Adapter的ListView的思路及代碼
- android開(kāi)發(fā)中ListView與Adapter使用要點(diǎn)介紹
- 舉例講解Android應(yīng)用中SimpleAdapter簡(jiǎn)單適配器的使用
- 詳解Android App中ViewPager使用PagerAdapter的方法
- Android控件系列之相冊(cè)Gallery&Adapter適配器入門(mén)&控件縮放動(dòng)畫(huà)入門(mén)
- Android中的Adapter簡(jiǎn)單介紹
- Android開(kāi)發(fā)中ListView自定義adapter的封裝
- Android listview與adapter詳解及實(shí)例代碼
- Adapter模式實(shí)戰(zhàn)之重構(gòu)鴻洋集團(tuán)的Android圓形菜單建行
- Android Adapter的幾個(gè)常用方法
- Android編程實(shí)現(xiàn)在adapter中進(jìn)行數(shù)據(jù)操作的方法
相關(guān)文章
Android中WebView加載的網(wǎng)頁(yè)被放大的解決辦法
這篇文章主要介紹了Android中WebView加載的網(wǎng)頁(yè)被放大的問(wèn)題的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
Android中EditText光標(biāo)的顯示與隱藏方法
這篇文章主要給大家介紹了關(guān)于Android中EditText光標(biāo)的顯示與隱藏以及Android之第一次不顯示EditText光標(biāo)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11
android使用webwiew載入頁(yè)面使用示例(Hybrid App開(kāi)發(fā))
Hybrid App 融合 Web App 的原理就是嵌入一個(gè)WebView組件,可以在這個(gè)組件中載入頁(yè)面,相當(dāng)于內(nèi)嵌的瀏覽器,下面是使用示例2014-03-03
Android中絕對(duì)音量和相對(duì)音量設(shè)置
大家好,本篇文章主要講的是Android中絕對(duì)音量和相對(duì)音量設(shè)置,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01

