我正在尝试覆盖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
的错误。造成这种情况的原因是什么,以及正确的解决办法是什么?
答案 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();
}
}