Android中BaseAdapter用法示例
程序员文章站
2024-03-06 15:55:44
本文实例讲述了android中baseadapter用法。分享给大家供大家参考,具体如下:
概述:
baseadapter就android应用程序中经常用到的基础数据适...
本文实例讲述了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程序设计有所帮助。