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編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android自定義viewgroup 使用adapter適配數(shù)據(jù)(6)
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
- Android自定義Adapter的ListView的思路及代碼
- android開發(fā)中ListView與Adapter使用要點(diǎn)介紹
- 舉例講解Android應(yīng)用中SimpleAdapter簡(jiǎn)單適配器的使用
- 詳解Android App中ViewPager使用PagerAdapter的方法
- Android控件系列之相冊(cè)Gallery&Adapter適配器入門&控件縮放動(dòng)畫入門
- Android中的Adapter簡(jiǎn)單介紹
- Android開發(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)文章
Flutter 滾動(dòng)監(jiān)聽及實(shí)戰(zhàn)appBar滾動(dòng)漸變的實(shí)現(xiàn)
這篇文章主要介紹了Flutter 滾動(dòng)監(jiān)聽及實(shí)戰(zhàn)appBar滾動(dòng)漸變,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09使用genymotion訪問本地上Tomcat上數(shù)據(jù)的方法
下面小編就為大家?guī)?lái)一篇使用genymotion訪問本地上Tomcat上數(shù)據(jù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-03-03理解關(guān)于Android系統(tǒng)中輕量級(jí)指針的實(shí)現(xiàn)
由于android系統(tǒng)底層的很大的一部分是用C++實(shí)現(xiàn)的,C++的開發(fā)就難免會(huì)使用到指針的這個(gè)知識(shí) 點(diǎn)。而C++的難點(diǎn)和容易出問題的也在于指針。使用指針出錯(cuò),常常會(huì)引發(fā)帶來(lái)對(duì)項(xiàng)目具有毀滅性的錯(cuò)誤,內(nèi)存泄漏、邏輯錯(cuò)誤、系統(tǒng)崩潰2021-10-10ViewPager和SlidingPaneLayout的滑動(dòng)事件沖突解決方法
下面小編就為大家分享一篇ViewPager和SlidingPaneLayout的滑動(dòng)事件沖突解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-01-01Android音視頻之視頻采集(系統(tǒng)API預(yù)覽)
這篇文章主要為大家詳細(xì)介紹了Android音視頻之視頻采集,系統(tǒng)API預(yù)覽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android 新聞界面模擬ListView和ViewPager的應(yīng)用
本文主要介紹 Android ListView和ViewPager的應(yīng)用,這里模擬了新聞界面及實(shí)現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09Android自定義View實(shí)現(xiàn)開關(guān)按鈕
android 自定義view知識(shí)非常廣泛,難以讓人掌握。但是也是andoroid進(jìn)階學(xué)習(xí)的必經(jīng)之路。下面通過本文給大家介紹Android自定義View實(shí)現(xiàn)開關(guān)按鈕的知識(shí),非常不錯(cuò),感興趣的朋友一起看看吧2016-11-11Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12