JF0001
JF0001

Reputation: 839

Custom SimpleCursorAdapter for Populating Rows of ListView

I am trying to define a Custom SimpleCursorAdapter Class, which will be used to populate the rows of a ListView. I am using the v4.widget.SimpleCursorAdapter library for backward compatibility. I am having issues with the following two parts of the code.

In the constructor, I am using

super(context,layout,c,from,to);

Meanwhile, this is deprecated, and I am not sure how to modify it.

Also, in

alarm_activated = (ToggleButton)row.findViewById(R.id.alarm_activated);

"row" cannot be resolved, and I am not sure how to reference the rows in question. (This syntax was working in a ArrayAdapter, and I was hoping that it would also work in a SimpleCursorAdapter...).

Perhaps, some other parts of my code (found hereafter) are incorrect.

    class AlarmRowAdapter extends SimpleCursorAdapter {

    private Context mContext;
    private Context appContext;
    private int layout;
    private Cursor cr;
    private final LayoutInflater inflater;

    public AlarmRowAdapter(Context context,int layout, Cursor c,String[] from,int[] to) {
        super(context,layout,c,from,to);
        this.layout=layout;
        this.mContext = context;
        this.inflater=LayoutInflater.from(context);
        this.cr=c;
    }

    @Override
    public View newView (Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(layout, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        alarm_activated = (ToggleButton)row.findViewById(R.id.alarm_activated);

        if (activationInt == 1) {
            alarm_activated.setChecked(true);
            alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
        } else {
            alarm_activated.setChecked(false);
        }

        alarm_activated.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    buttonView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
                } else {
                    buttonView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
                }
            }
        });

    }

}

Upvotes: 1

Views: 48

Answers (2)

JF0001
JF0001

Reputation: 839

Following suggestions made by Mike M. (please refer to comments above), here is the working code in case someone else would find it useful. Also, please note that I had used the example found at this link when developing my initial code (thank you to Bobbake4 for that code).

    class AlarmRowAdapter extends SimpleCursorAdapter {

    private Context mContext;
    private Context appContext;
    private int layout;
    private Cursor cr;
    private final LayoutInflater inflater;


    public AlarmRowAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context,layout,c,from,to, flags);
        this.layout=layout;
        this.mContext = context;
        this.inflater=LayoutInflater.from(context);
        this.cr=c;
    }

    @Override
    public View newView (Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(layout, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        alarm_activated = (ToggleButton)view.findViewById(R.id.alarm_activated);

        if (activationInt == 1) {
            alarm_activated.setChecked(true);
            alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
        } else {
            alarm_activated.setChecked(false);
        }

        alarm_activated.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    buttonView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
                } else {
                    buttonView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
                }
            }
        });

    }

}

Upvotes: 1

Nabin Bhandari
Nabin Bhandari

Reputation: 16379

For standard constructor add a flag parameter: CursorAdapter doc.

super (context,layout,c, from,to, 0);

And you are trying to use row.findViewById But the variable row doesn't exist. Since the parameter is view, you should use view.findViewById

Upvotes: 1

Related Questions