Subclassing SimpleCursorAdapter to include convertView for memory conservation

后端 未结 5 1717
抹茶落季
抹茶落季 2020-12-29 17:18

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 f

5条回答
  •  鱼传尺愫
    2020-12-29 17:48

    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!

    Here is an example of my implementation:

     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.

提交回复
热议问题