Android Adapter代码片

 /**
     * Adapter for grid of coupons.
     */
    private static class CouponAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private List<Coupon> mAllCoupons;

        /**
         * Constructs a new {@link CouponAdapter}.
         *
         * @param inflater to create new views
         * @param allCoupons for list of all coupons to be displayed
         */
        public CouponAdapter(LayoutInflater inflater, List<Coupon> allCoupons) {
            if (allCoupons == null) {
                throw new IllegalStateException("Can‘t have null list of coupons");
            }
            mAllCoupons = allCoupons;
            mInflater = inflater;
        }

        @Override
        public int getCount() {
            return mAllCoupons.size();
        }

        @Override
        public Coupon getItem(int position) {
            return mAllCoupons.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //缓存策略的另外一种写法

            View result = convertView;
            if (result == null) {
                //注意mInflater的来源,是在Activity的setContextView中这样写的:
                            // Fetch the {@link LayoutInflater} service so that new views can be created
                            //  LayoutInflater inflater = (LayoutInflater) getSystemService(
                            //                                        Context.LAYOUT_INFLATER_SERVICE);
                result = mInflater.inflate(R.layout.grid_item, parent, false);
            }

            // Try to get view cache or create a new one if needed
            ViewCache viewCache = (ViewCache) result.getTag();
            if (viewCache == null) {
                viewCache = new ViewCache(result);
                result.setTag(viewCache);
            }

            // Fetch item
            Coupon coupon = getItem(position);

            // Bind the data
            viewCache.mTitleView.setText(coupon.mTitle);
            viewCache.mSubtitleView.setText(coupon.mSubtitle);
            viewCache.mImageView.setImageURI(coupon.mImageUri);

            return result;
        }
    }

    /**
     * Cache of views in the grid item view to make recycling of views quicker. This avoids
     * additional {@link View#findViewById(int)} calls after the {@link ViewCache} is first
     * created for a view. See
     * {@link CouponAdapter#getView(int position, View convertView, ViewGroup parent)}.
     */
    private static class ViewCache {

        /** View that displays the title of the coupon */
        private final TextView mTitleView;

        /** View that displays the subtitle of the coupon */
        private final TextView mSubtitleView;

        /** View that displays the image associated with the coupon */
        private final ImageView mImageView;

        /**
         * Constructs a new {@link ViewCache}.
         *
         * @param view which contains children views that should be cached.
         */
        private ViewCache(View view) {
            mTitleView = (TextView) view.findViewById(R.id.title);
            mSubtitleView = (TextView) view.findViewById(R.id.subtitle);
            mImageView = (ImageView) view.findViewById(R.id.image);
        }
    }

    /**
     * 关于适配器里面数据bean对象问题,如果只是纯粹展示,而不需要改变bean对象的属性,那么推荐下面这种方式,如果需要改变
     * bean对象的属性,那么还是用常见的get set方法实现.
     */
    private static class Coupon {

        /** Title of the coupon. */
        private final String mTitle;

        /** Description of the coupon. */
        private final String mSubtitle;

        /** Content URI of the image for the coupon. */
        private final Uri mImageUri;

        /**
         * Constructs a new {@link Coupon}.
         *
         * @param titleString is the title
         * @param subtitleString is the description
         * @param imageAssetFilePath is the file path from the application‘s assets folder for
         *                           the image associated with this coupon
         */
        private Coupon(String titleString, String subtitleString, String imageAssetFilePath) {
            mTitle = titleString;
            mSubtitle = subtitleString;
            mImageUri = Uri.parse("content://" + AssetProvider.CONTENT_URI + "/" +
                    imageAssetFilePath);
        }
    }

  

时间: 2024-10-11 15:51:31

Android Adapter代码片的相关文章

Android Adapter详解

Android Adapter Adapter是用来帮助填充数据的中间桥梁,比如通过它将数据填充到ListView, GridView, Gallery.而android 提供了几种Adapter:ArrayAdapter, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter

[项目总结]论Android Adapter notifyDataSetChanged与notifyDataSetInvalidated无效原因

最近在开发中遇到一个问题,Adapter中使用notifyDataSetChanged 与notifyDataSetInvalidated无效,经过思考和网上查找,得出如下原因. 首先看一下notifyDataSetChanged与notifyDataSetInvalidated的区别 我们可应用粮仓来解释一下,两者的区别. notifyDataSetChanged:粮仓中得粮食少了,或者多了,发送通知. notifyDataSetInvalidated:粮仓变换了,比如原来从A仓取粮食,现在换

Android adapter适配器的使用

ListView之SimpleAdapter SimpleAdapter的构造函数是: public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) 参数context:上下文,比如this.关联SimpleAdapter运行的视图上下文 参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的ge

Android Adapter的一些记录

一.摘要 An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set. 这是Android官方对Adapter的

android通过代码控制ListView上下滚动

本文将介绍一种通过代码控制ListView上下滚动的方法. 先上图: 按下按钮会触发ListView滚动或停止. 实现该功能并不难,下面给出主要代码MainActivity.java package cn.guet.levide; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View;

android 实用代码片段整理

android 常用代码片段,前1-10条是从网上摘录,向原作者致谢.后面为自己整理. 1.设置窗口格式为半透明 getWindow().setFormat(PixelFormat.TRANSLUCENT); 2.Android中在非UI线程里更新View的不同方法: * Activity.runOnUiThread( Runnable ) * View.post( Runnable ) * View.postDelayed( Runnable, long ) * Hanlder 3.全屏显示窗

[转]Android Adapter以及getView()方法的理解

Android Adapter基本理解: 我的理解是: 1.一个有许多getter的类(就是getView(),getCount()....这些方法) 2.有多少个get方法?都是什么? 这些getter是特定的,你可以复写他们,全部的方法如下 其中一般我们只用复写getCount(),getView(),getItemId(),getItem()这四个方法 3.这些被谁调用? 这些getter是被android系统自行调用的(具体如何调用,作为像我这样的新手做稍微了解就好) 4.为什么要复写这

Android Studio代码着色插件

Android Studio代码着色插件 前言:半个多月没写博客了,放了个假期,这人才缓过来神,懒的啥都不想干,可算是明白一句话的意思了:玩物丧志啊!好在公司项目赶的紧,不想干活都不行,强行撸上正轨-- 正因为夜以继日的Coding赶模块,最近感觉眼睛干涩,眼圈红肿,看着那些颜色一层不变的abc,心里莫名的冒火,故有了本篇博文.还是建议大家敲一会abc后,起来走动走动,眼睛看看远处,让眼圈周围血液循环循环.不要像本屌,昨天回家路上买瓶水,付账时老板娘女儿问,你是不是搞IT的 ?!  ---- 此

Eclipse for android 实现代码自动提示智能提示功能

Eclipse for android 实现代码自动提示智能提示功能,介绍 Eclipse for android 编辑器中实现两种主要文件 java 与 xml 代码自动提示功能,解决 eclipse 代码提示失效.eclipse 不能自动提示.eclipse 没有代码提示的问题. 1.设置 java 文件的代码提示功能 打开 Eclipse 依次选择Window > Preferences > Java > Editor - Content Assist > Auto acti