user3087000
user3087000

Reputation: 751

Android - How to update Adapter's instance variable when the underlying data changed?

Following code will not work because inside onChanged method, we can not access the this because the two are different object.

How can I solve this problem? Please help me. Thanks.

this.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            //this will not work
            this.checkedStates = new ArrayList<CheckboxState>(this.getCount());
        }
 });

Upvotes: 0

Views: 61

Answers (1)

Lino
Lino

Reputation: 6160

if the code is inside an adapter and checkedStates is a class field, just use:

registerDataSetObserver(this);

then let your adapter implements DataSetObserver

@Override
public void onChanged(){
       super.onChanged();
       checkedStates = new ArrayList<CheckboxState>(getCount());
}

Upvotes: 1

Related Questions