Subclassing SimpleCursorAdapter to include convertView for memory conservation

后端 未结 5 1723
抹茶落季
抹茶落季 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:53

    You could also subclass ResourceCursorAdapter. In that case you only need to override the bindview method:

    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")));
        }
    }
    

    }

提交回复
热议问题