继承 SimpleCursorAdapter 以包含 convertView 以节省内存 [英] Subclassing SimpleCursorAdapter to include convertView for memory conservation

查看:22
本文介绍了继承 SimpleCursorAdapter 以包含 convertView 以节省内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览示例和教程,但我似乎无法理解如何在子类 SimpleCursorAdapter 中处理回收.我知道对于常规 ArrayAdapter,您可以检查 convertView 是否为 null,如果 null 来自 xml,则膨胀,如果不是 null,则回收,但我在可视化它如何与 SimpleCursorAdapter 子类中的 from 和 to 数组一起使用时遇到了一些麻烦.我试图从 Commonsware 的 The Busy Coders Guide to Android Development 中弄清楚这一点,但没有成功.如果有人知道任何提示、示例或教程,我将不胜感激.

I've been scouring throug the examples and tutorials but I can't seem to get my head around how to handle recycling within a subclassed SimpleCursorAdapter. I know that for regular ArrayAdapters you can check convertView for null and inflate if null from the xml and if not null, recycle, but I'm having a little trouble visualizing how that works with the from and to arrays within the SimpleCursorAdapter subclass. I tried to figure this out from the The Busy Coders Guide to Android Development by Commonsware but was unsuccessful. If anyone knows of any tips, examples, or tutorials, I would be grateful to see them.

推荐答案

我找不到任何很好的示例来将 SimpleCursorAdapter 子类化以使用 convertView 回收方法,因此我最终将 CursorAdapter 子类化.这实际上对我的实现非常有效,并且随着内存使用量的显着减少而飞速发展.回收视图确实有效,我强烈推荐它!

I was unable to find any good examples for Subclassing the SimpleCursorAdapter to use the convertView recycle methodolgy so I ended up subclassing CursorAdapter instead. This actually worked quite well for my implementation and is blazing fast with a dramatic decrease in memory usage. Recycling views really works and I recommend it highly!

这是我的实现示例:

 private static class MyNiftyAdapter extends CursorAdapter
    {
        private LayoutInflater mInflater;
        private Cursor cur;

        public MyNiftyAdapter(Context context, Cursor c) {
            super(context,c);       
            this.mInflater = LayoutInflater.from(context);
            this.cur = c;
        }
        public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
        {
            super(context, c, autoRequery);
            this.mInflater = LayoutInflater.from(context);
            this.cur = c;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder viewHolder;
            if(convertView == null)
            {
                convertView = this.mInflater.inflate(R.layout.chamber_item, null);
                viewHolder = new ViewHolder();
                viewHolder.name = (TextView)convertView.findViewById(R.id.Name);
                viewHolder.city = (TextView)convertView.findViewById(R.id.city);
                viewHolder.state = (TextView)convertView.findViewById(R.id.state);
                viewHolder.country = (TextView)convertView.findViewById(R.id.country);
                convertView.setTag(viewHolder);
            }else
            {
                viewHolder = (ViewHolder)convertView.getTag();
            }
            this.cur.moveToPosition(position);

            viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.NAME)));          
            viewHolder.city.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.CITY)));
            viewHolder.state.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.STATE)));
            viewHolder.country.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.COUNTRY)));            

            return convertView;
        }
        /* (non-Javadoc)
         * @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
         */
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // Dont need to do anything here

        }
        /* (non-Javadoc)
         * @see android.widget.CursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
         */
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // Dont need to do anything here either
            return null;
        }

        static class ViewHolder
        {
            TextView name;
            TextView city;
            TextView state;
            TextView country;
        }
    }

我的列表中的每一行现在都完全按照我的需要显示名称、城市、州和国家.希望这会有所帮助.

Each row in my list now displays name, city, state, and country exactly as I wanted. Hope this helps.

这篇关于继承 SimpleCursorAdapter 以包含 convertView 以节省内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆