Android中BaseAdapter用法示例
本文實例講述了Android中BaseAdapter用法。分享給大家供大家參考,具體如下:
概述:
BaseAdapter就Android應用程序中經常用到的基礎數(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(); } }
其中兩個注意點為:
setTag 用View設置存儲數(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>
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android視圖View技巧總結》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
- Android自定義viewgroup 使用adapter適配數(shù)據(jù)(6)
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
- Android自定義Adapter的ListView的思路及代碼
- android開發(fā)中ListView與Adapter使用要點介紹
- 舉例講解Android應用中SimpleAdapter簡單適配器的使用
- 詳解Android App中ViewPager使用PagerAdapter的方法
- Android控件系列之相冊Gallery&Adapter適配器入門&控件縮放動畫入門
- Android中的Adapter簡單介紹
- Android開發(fā)中ListView自定義adapter的封裝
- Android listview與adapter詳解及實例代碼
- Adapter模式實戰(zhàn)之重構鴻洋集團的Android圓形菜單建行
- Android Adapter的幾個常用方法
- Android編程實現(xiàn)在adapter中進行數(shù)據(jù)操作的方法
相關文章
Flutter 滾動監(jiān)聽及實戰(zhàn)appBar滾動漸變的實現(xiàn)
這篇文章主要介紹了Flutter 滾動監(jiān)聽及實戰(zhàn)appBar滾動漸變,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09使用genymotion訪問本地上Tomcat上數(shù)據(jù)的方法
下面小編就為大家?guī)硪黄褂胓enymotion訪問本地上Tomcat上數(shù)據(jù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03理解關于Android系統(tǒng)中輕量級指針的實現(xiàn)
由于android系統(tǒng)底層的很大的一部分是用C++實現(xiàn)的,C++的開發(fā)就難免會使用到指針的這個知識 點。而C++的難點和容易出問題的也在于指針。使用指針出錯,常常會引發(fā)帶來對項目具有毀滅性的錯誤,內存泄漏、邏輯錯誤、系統(tǒng)崩潰2021-10-10ViewPager和SlidingPaneLayout的滑動事件沖突解決方法
下面小編就為大家分享一篇ViewPager和SlidingPaneLayout的滑動事件沖突解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android音視頻之視頻采集(系統(tǒng)API預覽)
這篇文章主要為大家詳細介紹了Android音視頻之視頻采集,系統(tǒng)API預覽,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android 新聞界面模擬ListView和ViewPager的應用
本文主要介紹 Android ListView和ViewPager的應用,這里模擬了新聞界面及實現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09Android自定義TimeButton實現(xiàn)倒計時按鈕
這篇文章主要為大家詳細介紹了Android自定義TimeButton實現(xiàn)倒計時按鈕,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12