user2676468
user2676468

Reputation:

Overriding notifyDataSetChanged() not working

I'm trying to override notifyDataSetChanged to simply call super.notifyDataSetChanged, but I want to force it to happen on the main UI thread.

I have

@Override
    public void notifyDataSetChanged() {

        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                super.notifyDataSetChanged();

            }
        });
    }

But I get an error that The method notifyDataSetChanged() is undefined for the type Object. What is causing this and what is the proper fix?

Upvotes: 0

Views: 288

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134664

Assuming your adapter class is structured something like this:

public class MyAdapter extends BaseAdapter {
    private Activity mActivity;

    public MyAdapter(Activity activity) {
        super(activity);
        mActivity = activity;
    }

    @Override
    public void notifyDataSetChanged() {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                super.notifyDataSetChanged();
            }
        }
    }
}

At the point that you call super.notifyDataSetChanged(), you're in the context of the new anonymous Runnable class, so calling super is trying to find the superclass of that Runnable; not the Adapter, which is what you're expecting.

You need to qualify that you're looking for the Adapter class's superclass:

mActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        MyAdapter.super.notifyDataSetChanged();
    }
}

Upvotes: 2

Related Questions