覆盖notifyDataSetChanged()不起作用

时间:2014-06-27 16:57:25

标签: java android android-adapter

我正在尝试覆盖notifyDataSetChanged以简单地调用super.notifyDataSetChanged,但我想强制它在主UI线程上发生。

我有

@Override
    public void notifyDataSetChanged() {

        activity.runOnUiThread(new Runnable() {

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

            }
        });
    }

但我收到The method notifyDataSetChanged() is undefined for the type Object的错误。造成这种情况的原因是什么,以及正确的解决办法是什么?

1 个答案:

答案 0 :(得分:2)

假设您的适配器类的结构如下:

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

当您调用super.notifyDataSetChanged()时,您处于新的匿名Runnable类的上下文中,因此调用super正在尝试查找该Runnable的超类;不是适配器,这是你期待的。

您需要确定您正在寻找Adapter班级的超类:

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