子类化 SimpleCursorAdapter 以包含 convertView 以节省内存

Posted

技术标签:

【中文标题】子类化 SimpleCursorAdapter 以包含 convertView 以节省内存【英文标题】:Subclassing SimpleCursorAdapter to include convertView for memory conservation 【发布时间】:2011-05-05 08:55:25 【问题描述】:

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

【问题讨论】:

虽然这已经过时了,但对于那些在未来查看它的人来说:SimpleCursorAdapter 的代码不会覆盖 getView;相反,在 getView(在 CursorAdapter 中)检查 convertView 之后,它会覆盖 bindView。 (代码:android.googlesource.com/platform/frameworks/base/+/refs/heads/…) 【参考方案1】:

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

这是我的实现示例:

 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;
        
    

现在,我列表中的每一行都完全按照我的意愿显示名称、城市、州和国家/地区。希望这会有所帮助。

【讨论】:

新的SimpleCursorAdapter 构造函数还需要这个吗?我很好奇,因为我认为它不是,但我的ListView 的性能是可怕的。 我认为你的做法不对。除了重写不是抽象方法的getView(),您可以简单地重写newView()bindView() 函数来创建新视图或使用以前创建的视图。 CursorAdapter 将负责创建新视图或在必要时回收先前创建的视图。更少的代码,更干净,实际上是标准的方式!【参考方案2】:

CursorAdapter 为您完成部分工作。只需要在需要创建新视图时重写 newView(),在回收现有视图时重写 bindView()。

【讨论】:

你知道我在哪里可以看到一个例子吗?【参考方案3】:



import java.util.ArrayList;

import android.content.Context; import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; import android.widget.ImageView; import android.widget.TextView;

public class AccountsAdapter extends CursorAdapter

 public static final String TAG = AccountsAdapter.class.getSimpleName();

 private Boolean DEBUG=true;
 private LayoutInflater mInflater;
 private int layoutResource;
 private Cursor mCursor;
 private int acctInd;
 private int upicInd;
 private  ViewHolder holder;
 public AccountsAdapter(Context context,Cursor c,int resourceId) 
     super(context,c);
     this.mCursor=c;
     acctInd=mCursor.getColumnIndex(LJDB.KEY_JOURNALNAME);
     upicInd=mCursor.getColumnIndex(LJDB.KEY_DEFAULTUSERPIC);

     this.layoutResource=resourceId;
     this.mInflater = LayoutInflater.from(context);
 


 private static class ViewHolder 
     ImageView userpic;
     TextView journalname;
 

@Override public void bindView(View view, Context context, Cursor cursor) holder = (ViewHolder) view.getTag(); holder.journalname.setText(cursor.getString(acctInd)); holder.userpic.setTag(cursor.getString(upicInd));

@Override public View newView(Context context, Cursor cursor, ViewGroup parent) View v = mInflater.inflate(layoutResource, null); holder = new ViewHolder(); holder.userpic = (ImageView) v.findViewById(R.id.duserpic); holder.journalname = (TextView) v.findViewById(R.id.uname);

v.setTag(holder); return v;

【讨论】:

【参考方案4】:

你也可以继承ResourceCursorAdapter。在这种情况下,您只需要覆盖 bindview 方法:

public class MySimpleCursorAdapter extends ResourceCursorAdapter 

public MySimpleCursorAdapter(Context context, Cursor c) 
    super(context, R.layout.myLayout, c);


@Override
public void bindView(View view, Context context, Cursor cursor) 
    ViewHolder viewHolder = (ViewHolder) view.getTag();
    if (viewHolder == null) 
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
    
    viewHolder.bind(cursor, context);


/**
 * A ViewHolder keeps references to children views to avoid unnecessary calls
 * to findViewById() on each row (especially during scrolling)
 */
private static class ViewHolder 
    private TextView text;

    private ToggleButton toggle;

    public ViewHolder(View view) 
        text = (TextView) view.findViewById(R.id.rowText);
        toggle = (ToggleButton) view.findViewById(R.id.rowToggleButton);
    

    /**
     * Bind the data from the cursor to the proper views that are hold in
     * this holder
     * @param cursor
     */
    public void bind(Cursor cursor, Context context) 
        toggle.setChecked(0 != cursor.getInt(cursor.getColumnIndex("ENABLED")));
        text.setText(cursor.getString(cursor.getColumnIndex("TEXT")));
    

【讨论】:

【参考方案5】:

根据我的测试,接受的答案似乎不正确(更何况,不必要)。

据我所知(基于简单的 Log.e 输出测试),默认情况下会正确处理视图回收(如@Romain Guy 所述)。我认为这可以证明没有建议您需要在我遇到的任何文档中覆盖 getView()

这是我用来得出这个结论的简单示例中使用的代码sn-p:

public class MediaAdapter extends CursorAdapter 

    @Override
    public void bindView(View arg0, Context arg1, Cursor arg2) 
        Log.e("bindView", "Called: " + arg2.getPosition());
        ImageView iv = (ImageView) arg0;
        iv.setImageDrawable(new BitmapDrawable(getResources(), MediaStore.Images.Thumbnails.getThumbnail(arg1.getContentResolver(), 
                arg2.getInt(arg2.getColumnIndex(MediaStore.Images.Media._ID)), MediaStore.Images.Thumbnails.MICRO_KIND, null)));

    

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) 
        Log.e("newView", "Called: " + arg1.getPosition());
        ImageView iv = new ImageView(ctFrag);
        iv.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, 96));
        iv.setPadding(Spine.getDp(2), Spine.getDp(2), Spine.getDp(2), Spine.getDp(2));

        return iv;
    


当在 GridView 中使用它来显示可用的图像缩略图网格时,LogCat 输出如下:

06-11 21:50:09.906: E/newView(3192): Called: 0
06-11 21:50:09.906: E/bindView(3192): Called: 0
06-11 21:50:09.929: E/bindView(3192): Called: 0
06-11 21:50:09.937: E/bindView(3192): Called: 0
06-11 21:50:09.945: E/bindView(3192): Called: 0
06-11 21:50:09.953: E/bindView(3192): Called: 0
06-11 21:50:09.968: E/bindView(3192): Called: 0
06-11 21:50:09.984: E/bindView(3192): Called: 0
06-11 21:50:10.000: D/dalvikvm(3192): GC_CONCURRENT freed 555K, 5% free 14670K/15303K, paused 1ms+6ms
06-11 21:50:10.023: E/bindView(3192): Called: 0
06-11 21:50:10.031: E/bindView(3192): Called: 0
06-11 21:50:10.047: E/newView(3192): Called: 1
06-11 21:50:10.047: E/bindView(3192): Called: 1
06-11 21:50:10.062: E/newView(3192): Called: 2
06-11 21:50:10.062: E/bindView(3192): Called: 2
06-11 21:50:10.070: E/newView(3192): Called: 3
06-11 21:50:10.070: E/bindView(3192): Called: 3
06-11 21:50:10.078: E/newView(3192): Called: 4
06-11 21:50:10.078: E/bindView(3192): Called: 4
06-11 21:50:10.086: E/newView(3192): Called: 5
06-11 21:50:10.086: E/bindView(3192): Called: 5
06-11 21:50:10.093: E/newView(3192): Called: 6
06-11 21:50:10.093: E/bindView(3192): Called: 6
06-11 21:50:10.093: E/newView(3192): Called: 7
06-11 21:50:10.093: E/bindView(3192): Called: 7
06-11 21:50:10.101: E/newView(3192): Called: 8
06-11 21:50:10.101: E/bindView(3192): Called: 8
06-11 21:50:10.101: E/newView(3192): Called: 9
06-11 21:50:10.101: E/bindView(3192): Called: 9
06-11 21:50:10.125: E/newView(3192): Called: 10
06-11 21:50:10.125: E/bindView(3192): Called: 10
06-11 21:50:10.133: E/newView(3192): Called: 11
06-11 21:50:10.133: E/bindView(3192): Called: 11
06-11 21:50:10.140: D/dalvikvm(3192): GC_CONCURRENT freed 351K, 3% free 15001K/15431K, paused 1ms+2ms
06-11 21:50:10.140: E/newView(3192): Called: 12
06-11 21:50:10.140: E/bindView(3192): Called: 12
06-11 21:50:10.164: E/newView(3192): Called: 13
06-11 21:50:10.164: E/bindView(3192): Called: 13
06-11 21:50:10.179: E/newView(3192): Called: 14
06-11 21:50:10.179: E/bindView(3192): Called: 14
06-11 21:50:10.187: E/newView(3192): Called: 15
06-11 21:50:10.187: E/bindView(3192): Called: 15
06-11 21:50:10.195: E/newView(3192): Called: 16
06-11 21:50:10.195: E/bindView(3192): Called: 16
06-11 21:50:10.203: E/newView(3192): Called: 17
06-11 21:50:10.203: E/bindView(3192): Called: 17
06-11 21:50:10.211: E/newView(3192): Called: 18
06-11 21:50:10.211: E/bindView(3192): Called: 18
06-11 21:50:10.218: E/newView(3192): Called: 19
06-11 21:50:10.218: E/bindView(3192): Called: 19
06-11 21:50:10.218: E/newView(3192): Called: 20
06-11 21:50:10.218: E/bindView(3192): Called: 20
06-11 21:50:10.218: E/newView(3192): Called: 21
06-11 21:50:10.218: E/bindView(3192): Called: 21
06-11 21:50:10.226: E/newView(3192): Called: 22
06-11 21:50:10.226: E/bindView(3192): Called: 22
06-11 21:50:10.226: E/newView(3192): Called: 23
06-11 21:50:10.226: E/bindView(3192): Called: 23
06-11 21:50:10.234: E/newView(3192): Called: 24
06-11 21:50:10.234: E/bindView(3192): Called: 24
06-11 21:50:10.234: E/newView(3192): Called: 25
06-11 21:50:10.234: E/bindView(3192): Called: 25
06-11 21:50:10.242: E/newView(3192): Called: 26
06-11 21:50:10.242: E/bindView(3192): Called: 26
06-11 21:50:10.242: E/newView(3192): Called: 27
06-11 21:50:10.242: E/bindView(3192): Called: 27
06-11 21:50:10.250: D/dalvikvm(3192): GC_CONCURRENT freed 247K, 2% free 15582K/15879K, paused 1ms+1ms
06-11 21:50:10.250: E/newView(3192): Called: 28
06-11 21:50:10.250: E/bindView(3192): Called: 28
06-11 21:50:10.265: E/newView(3192): Called: 29
06-11 21:50:10.265: E/bindView(3192): Called: 29
06-11 21:50:10.265: E/newView(3192): Called: 30
06-11 21:50:10.265: E/bindView(3192): Called: 30
06-11 21:50:10.273: E/newView(3192): Called: 31
06-11 21:50:10.273: E/bindView(3192): Called: 31
06-11 21:50:10.273: E/newView(3192): Called: 32
06-11 21:50:10.273: E/bindView(3192): Called: 32
06-11 21:50:10.281: E/newView(3192): Called: 33
06-11 21:50:10.281: E/bindView(3192): Called: 33
06-11 21:50:10.281: E/newView(3192): Called: 34
06-11 21:50:10.281: E/bindView(3192): Called: 34
06-11 21:50:10.289: E/newView(3192): Called: 35
06-11 21:50:10.289: E/bindView(3192): Called: 35
06-11 21:50:10.289: E/newView(3192): Called: 36
06-11 21:50:10.289: E/bindView(3192): Called: 36
06-11 21:50:10.297: E/newView(3192): Called: 37
06-11 21:50:10.297: E/bindView(3192): Called: 37
06-11 21:50:10.297: E/newView(3192): Called: 38
06-11 21:50:10.297: E/bindView(3192): Called: 38
06-11 21:50:10.297: E/newView(3192): Called: 39
06-11 21:50:10.297: E/bindView(3192): Called: 39
06-11 21:50:10.304: E/newView(3192): Called: 40
06-11 21:50:10.304: E/bindView(3192): Called: 40
06-11 21:50:10.304: E/newView(3192): Called: 41
06-11 21:50:10.304: E/bindView(3192): Called: 41
06-11 21:50:10.312: E/newView(3192): Called: 42
06-11 21:50:10.312: E/bindView(3192): Called: 42
06-11 21:50:10.312: E/newView(3192): Called: 43
06-11 21:50:10.312: E/bindView(3192): Called: 43
06-11 21:50:10.320: E/newView(3192): Called: 44
06-11 21:50:10.320: E/bindView(3192): Called: 44
06-11 21:50:10.336: E/newView(3192): Called: 45
06-11 21:50:10.336: E/bindView(3192): Called: 45
06-11 21:50:10.343: E/newView(3192): Called: 46
06-11 21:50:10.343: E/bindView(3192): Called: 46
06-11 21:50:10.351: D/dalvikvm(3192): GC_CONCURRENT freed 301K, 3% free 16297K/16647K, paused 1ms+3ms
06-11 21:50:10.351: E/newView(3192): Called: 47
06-11 21:50:10.351: E/bindView(3192): Called: 47
06-11 21:50:10.351: E/newView(3192): Called: 48
06-11 21:50:10.359: E/bindView(3192): Called: 48
06-11 21:50:10.367: E/newView(3192): Called: 49
06-11 21:50:10.367: E/bindView(3192): Called: 49
06-11 21:50:10.383: E/newView(3192): Called: 50
06-11 21:50:10.383: E/bindView(3192): Called: 50
06-11 21:50:10.390: E/newView(3192): Called: 51
06-11 21:50:10.390: E/bindView(3192): Called: 51
06-11 21:50:10.406: E/newView(3192): Called: 52
06-11 21:50:10.406: E/bindView(3192): Called: 52
06-11 21:50:10.414: E/newView(3192): Called: 53
06-11 21:50:10.414: E/bindView(3192): Called: 53
06-11 21:50:10.422: E/newView(3192): Called: 54
06-11 21:50:10.422: E/bindView(3192): Called: 54
06-11 21:50:10.422: E/newView(3192): Called: 55
06-11 21:50:10.422: E/bindView(3192): Called: 55
06-11 21:50:10.429: E/newView(3192): Called: 56
06-11 21:50:10.429: E/bindView(3192): Called: 56
06-11 21:50:10.437: E/newView(3192): Called: 57
06-11 21:50:10.437: E/bindView(3192): Called: 57
06-11 21:50:10.453: E/newView(3192): Called: 58
06-11 21:50:10.453: E/bindView(3192): Called: 58
06-11 21:50:10.461: E/newView(3192): Called: 59
06-11 21:50:10.461: E/bindView(3192): Called: 59
06-11 21:50:10.468: E/newView(3192): Called: 60
06-11 21:50:10.468: E/bindView(3192): Called: 60
06-11 21:50:10.484: E/newView(3192): Called: 61
06-11 21:50:10.484: E/bindView(3192): Called: 61
06-11 21:50:10.492: E/newView(3192): Called: 62
06-11 21:50:10.492: E/bindView(3192): Called: 62
06-11 21:50:10.508: E/newView(3192): Called: 63
06-11 21:50:10.508: E/bindView(3192): Called: 63
06-11 21:50:10.515: E/newView(3192): Called: 64
06-11 21:50:10.515: E/bindView(3192): Called: 64
06-11 21:50:10.523: E/newView(3192): Called: 65
06-11 21:50:10.523: E/bindView(3192): Called: 65
06-11 21:50:10.539: E/newView(3192): Called: 0
06-11 21:50:10.539: E/bindView(3192): Called: 0
06-11 21:50:10.547: E/bindView(3192): Called: 0
06-11 21:50:10.547: E/bindView(3192): Called: 0
06-11 21:50:10.578: E/bindView(3192): Called: 0
06-11 21:50:10.593: D/dalvikvm(3192): GC_CONCURRENT freed 392K, 3% free 17115K/17543K, paused 2ms+7ms
06-11 21:50:10.601: E/bindView(3192): Called: 0
06-11 21:50:10.609: E/bindView(3192): Called: 0
06-11 21:50:10.625: E/bindView(3192): Called: 0
06-11 21:50:10.633: E/bindView(3192): Called: 0
06-11 21:50:14.586: D/dalvikvm(3192): GC_FOR_ALLOC freed 307K, 4% free 18033K/18695K, paused 14ms
06-11 21:50:14.632: D/dalvikvm(3192): GC_FOR_ALLOC freed 2K, 3% free 19439K/19911K, paused 11ms
06-11 21:50:14.632: E/bindView(3192): Called: 66
06-11 21:50:14.640: E/bindView(3192): Called: 67
06-11 21:50:14.640: E/bindView(3192): Called: 68
06-11 21:50:14.648: E/bindView(3192): Called: 69
06-11 21:50:14.648: E/bindView(3192): Called: 70
06-11 21:50:14.656: E/bindView(3192): Called: 71
06-11 21:50:14.656: E/bindView(3192): Called: 72
06-11 21:50:14.664: E/bindView(3192): Called: 73
06-11 21:50:14.664: E/bindView(3192): Called: 74
06-11 21:50:14.672: E/bindView(3192): Called: 75
06-11 21:50:14.672: E/bindView(3192): Called: 76
06-11 21:50:14.679: E/bindView(3192): Called: 77
06-11 21:50:14.679: E/bindView(3192): Called: 78
06-11 21:50:14.687: E/newView(3192): Called: 79
06-11 21:50:14.687: E/bindView(3192): Called: 79
06-11 21:50:14.687: E/newView(3192): Called: 80
06-11 21:50:14.687: E/bindView(3192): Called: 80
06-11 21:50:14.687: E/newView(3192): Called: 81
06-11 21:50:14.687: E/bindView(3192): Called: 81
06-11 21:50:14.695: E/newView(3192): Called: 82
06-11 21:50:14.695: E/bindView(3192): Called: 82
06-11 21:50:14.695: E/newView(3192): Called: 83
06-11 21:50:14.695: E/bindView(3192): Called: 83
06-11 21:50:14.750: E/bindView(3192): Called: 84
06-11 21:50:14.750: E/bindView(3192): Called: 85
06-11 21:50:14.757: E/bindView(3192): Called: 86
06-11 21:50:14.757: E/bindView(3192): Called: 87
06-11 21:50:14.765: E/bindView(3192): Called: 88
06-11 21:50:14.765: E/bindView(3192): Called: 89
06-11 21:50:14.812: E/bindView(3192): Called: 90
06-11 21:50:14.820: E/bindView(3192): Called: 91
06-11 21:50:14.820: E/bindView(3192): Called: 92
06-11 21:50:14.828: E/bindView(3192): Called: 93
06-11 21:50:14.828: E/bindView(3192): Called: 94
06-11 21:50:14.828: E/bindView(3192): Called: 95
06-11 21:50:14.851: D/dalvikvm(3192): GC_CONCURRENT freed 1350K, 7% free 20078K/21511K, paused 2ms+3ms
06-11 21:50:14.882: E/bindView(3192): Called: 96
06-11 21:50:14.890: E/bindView(3192): Called: 97
06-11 21:50:14.890: E/bindView(3192): Called: 98
06-11 21:50:14.890: E/bindView(3192): Called: 99
06-11 21:50:14.898: E/bindView(3192): Called: 100
06-11 21:50:14.898: E/bindView(3192): Called: 101
06-11 21:50:14.945: E/bindView(3192): Called: 102
06-11 21:50:14.945: E/bindView(3192): Called: 103
06-11 21:50:14.953: E/bindView(3192): Called: 104
06-11 21:50:14.953: E/bindView(3192): Called: 105
06-11 21:50:14.961: E/bindView(3192): Called: 106
06-11 21:50:14.961: E/bindView(3192): Called: 107
06-11 21:50:15.078: E/bindView(3192): Called: 108
06-11 21:50:15.086: E/bindView(3192): Called: 109
06-11 21:50:15.086: E/bindView(3192): Called: 110
06-11 21:50:15.093: E/bindView(3192): Called: 111
06-11 21:50:15.093: E/bindView(3192): Called: 112
06-11 21:50:15.101: E/bindView(3192): Called: 113
06-11 21:50:15.711: E/bindView(3192): Called: 114
06-11 21:50:15.718: E/bindView(3192): Called: 115
06-11 21:50:15.718: E/bindView(3192): Called: 116
06-11 21:50:15.726: E/bindView(3192): Called: 117
06-11 21:50:15.726: E/bindView(3192): Called: 118
06-11 21:50:15.734: E/bindView(3192): Called: 119
06-11 21:50:15.734: E/bindView(3192): Called: 120
06-11 21:50:15.742: E/bindView(3192): Called: 121
06-11 21:50:15.742: E/bindView(3192): Called: 122
06-11 21:50:15.750: E/bindView(3192): Called: 123
06-11 21:50:15.750: E/bindView(3192): Called: 124
06-11 21:50:15.757: E/bindView(3192): Called: 125
06-11 21:50:15.867: E/bindView(3192): Called: 126
06-11 21:50:15.867: E/bindView(3192): Called: 127
06-11 21:50:15.875: E/bindView(3192): Called: 128
06-11 21:50:15.875: E/bindView(3192): Called: 129
06-11 21:50:15.882: E/bindView(3192): Called: 130
06-11 21:50:15.882: E/bindView(3192): Called: 131
06-11 21:50:15.922: E/bindView(3192): Called: 132
06-11 21:50:15.929: E/bindView(3192): Called: 133
06-11 21:50:15.929: E/bindView(3192): Called: 134
06-11 21:50:15.953: E/bindView(3192): Called: 135
06-11 21:50:15.961: E/bindView(3192): Called: 136
06-11 21:50:15.961: E/bindView(3192): Called: 137
06-11 21:50:15.968: D/dalvikvm(3192): GC_CONCURRENT freed 2012K, 10% free 20197K/22215K, paused 2ms+3ms
06-11 21:50:15.968: E/bindView(3192): Called: 138
06-11 21:50:15.976: E/bindView(3192): Called: 139
06-11 21:50:15.984: E/bindView(3192): Called: 140
06-11 21:50:15.984: E/bindView(3192): Called: 141
06-11 21:50:15.992: E/bindView(3192): Called: 142
06-11 21:50:15.992: E/bindView(3192): Called: 143
06-11 21:50:16.039: E/bindView(3192): Called: 144
06-11 21:50:16.039: E/bindView(3192): Called: 145
06-11 21:50:16.047: E/bindView(3192): Called: 146
06-11 21:50:16.047: E/bindView(3192): Called: 147
06-11 21:50:16.054: E/bindView(3192): Called: 148
06-11 21:50:16.054: E/bindView(3192): Called: 149
06-11 21:50:16.062: E/bindView(3192): Called: 150
06-11 21:50:16.062: E/bindView(3192): Called: 151
06-11 21:50:16.062: E/bindView(3192): Called: 152
06-11 21:50:16.070: E/bindView(3192): Called: 153
06-11 21:50:16.070: E/bindView(3192): Called: 154
06-11 21:50:16.070: E/bindView(3192): Called: 155
06-11 21:50:16.117: E/bindView(3192): Called: 156
06-11 21:50:16.117: E/bindView(3192): Called: 157
06-11 21:50:16.125: E/bindView(3192): Called: 158
06-11 21:50:16.125: E/bindView(3192): Called: 159
06-11 21:50:16.125: E/bindView(3192): Called: 160
06-11 21:50:16.132: E/bindView(3192): Called: 161
06-11 21:50:16.172: E/bindView(3192): Called: 162
06-11 21:50:16.172: E/bindView(3192): Called: 163
06-11 21:50:16.179: E/bindView(3192): Called: 164
06-11 21:50:16.179: E/bindView(3192): Called: 165
06-11 21:50:16.187: E/bindView(3192): Called: 166
06-11 21:50:16.187: E/bindView(3192): Called: 167
06-11 21:50:16.328: E/bindView(3192): Called: 168
06-11 21:50:16.336: E/bindView(3192): Called: 169
06-11 21:50:16.336: E/bindView(3192): Called: 170
06-11 21:50:16.343: E/bindView(3192): Called: 171
06-11 21:50:16.343: E/bindView(3192): Called: 172
06-11 21:50:16.351: E/bindView(3192): Called: 173
06-11 21:50:16.461: E/bindView(3192): Called: 174
06-11 21:50:16.468: E/bindView(3192): Called: 175
06-11 21:50:16.492: E/bindView(3192): Called: 176
06-11 21:50:16.492: E/bindView(3192): Called: 177

这似乎很清楚地表明,一旦通过调用 newView() 创建了视图,bindView() 就会处理转换视图以优化内存。

(在中间调用 newView() 的几行只是 GridView 在显示一行的两个部分而不是一个整体时需要一个新行)。

【讨论】:

我只使用了一个 SimpleCursorAdapter 而没有覆盖任何东西。我的观点只是 TextViews。我确实看过 Romain Guy 的 listview 视频,他说有一种不好的方法来覆盖 getView,但我没有覆盖它,所以不覆盖它是不是很糟糕? 不,不覆盖已经写好的东西总是不错的。这里的这个问题更多地是在讨论覆盖已经实现的内存优化方法的结果。覆盖已经写好的东西从来都不是坏事(意识到全面的声明可能会遇到例外......)。如果已经存在的功能不能满足您的要求,通常会进行覆盖以扩展功能。 @flobacca 能否请您发布该视频的链接? @Lai Vung 哎呀,我很确定我在谈论 Google I/O 2010 - youtube.com/watch?v=wDBM6wVEO70 的 ListView 世界 但是,我正在听的部分说速度很慢方式,中方式和快速方式。我想没有“坏”的方式,但慢也一样糟糕。 @Lai Vung : 8:40/59:43 谈慢路。好吧,看起来你已经看过视频了。

以上是关于子类化 SimpleCursorAdapter 以包含 convertView 以节省内存的主要内容,如果未能解决你的问题,请参考以下文章

使用详解及源码解析Android中的AdapterBaseAdapterArrayAdapterSimpleAdapter和SimpleCursorAdapter

Android-ListView-SimpleCursorAdapter

使用 SimpleCursorAdapter 从 Cursor 更改值

如何子类化字典以支持泛型类型提示?

以编程方式实例化 TableViewController 子类后,它的出口为零

想知道如何以编程方式在 swift 3.0 中对 UITableViewCell 进行子类化?