我有一个抽象的MonthAdapter类
MonthAdapter.java
public MonthAdapter(Context c, int month, int year, DisplayMetrics metrics) {
this.mContext = c;
this.mMonth = month;
this.mYear = year;
this.mCalendar = new GregorianCalendar(mYear, mMonth, 1);
this.mCalendarToday = Calendar.getInstance();
this.mDisplayMetrics = metrics;
populateMonth();
}
protected abstract void onDate(int[] date, int position, View item);
}
我想在MainActivity的gridview适配器上实现它,如下所示:
mGridView.setAdapter(new MonthAdapter(this, param[1], param[2], metrics));
如何实现此抽象类,因为抽象类无法在MainActivity中实例化,并且主类也不能直接继承到抽象类,因为MainActivity已经扩展到Activity。 谢谢你的答案。
答案 0 :(得分:0)
mGridView.setAdapter(new MonthAdapter(this, param[1], param[2], metrics){
@Override
public void onDate(int[] date, int position, View item){
}
);
答案 1 :(得分:0)
您可以创建一个扩展抽象类的类,例如:
public class MonthAdapterImpl extends MonthAdapter {
public MonthAdapter(Context c, int month, int year, DisplayMetrics metrics) {
super(c, month, year, metrics);
}
@Override
protected abstract void onDate(int[] date, int position, View item) {
// the code
};
}
希望这有帮助。