子类&超类使用(Android)

时间:2013-10-18 02:58:31

标签: android arraylist subclass extend superclass

背景:我有一个相当受欢迎的小众应用,我希望通过创建一个通用代码的主库来扩展,我可以使用它来生成更多,类似的应用程序,只需要很少的工作。

超级课程(在图书馆中): BaseTheme

子类(单独的应用): 主题(扩展BaseTheme,为每个应用程序提供一些特定的方法)。 比较

在Comparator课程中,我有一个Arraylist。我想将Comparator移动到库中,但是这样做,Arraylist抛出一个错误,因为库中不存在Theme

我尝试将Arraylist改为Arraylist,认为Theme是BaseTheme的扩展,并且允许但是当我试图在Arraylist中创建一个主题时,它不起作用。

在应用代码中:

private ArrayList<Theme> mThemes;
private MyComparator mComparator;

mThemes = mComparator.compare(mSortType, mSortAorD, true, mThemes);

错误:MyComparator类型中的方法compare(int,int,Boolean,ArrayList)不适用于参数(int,int,boolean,ArrayList) - 我相信这是因为比较器的构造函数具有ArrayList,因为库代码中不存在Theme

有没有办法让这种情况发生,或者我是否需要保持它当前的状态(应用程序代码中的比较者)?

图书馆代码:         公共课MyComparator {

private static Context mCtx;


public MyComparator(Context ctx) {
  mCtx = ctx;

}
public ArrayList<BaseTheme> compare(final int type, final int sort, Boolean flurry, ArrayList<BaseTheme> list) {
    Comparator<BaseTheme> myComparator = getComparator(Sort.SORT_ALPHABETICAL, Sort.DESC, flurry);
    Collections.sort(list, myComparator);

    myComparator = getComparator(type, sort, flurry);
    Collections.sort(list, myComparator);
    return list;
}

public abstract class BaseTheme {
public String mName;
public String mCreator;
public String mDate;
public String mAddress;
public int mThemeID;
public int mPopularity;
public boolean mIsFavorite;
public boolean mIsVisible;
public Context mCtx;

public BaseTheme(int themeID, String name, String creator, String date, String address, Boolean favorite, 
        Boolean visible, int popularity, boolean isFavorite, boolean isVisible, Context ctx) {

    setThemeID(themeID);
    setName(name);
    setCreator(creator);
    setDate(date);
    setAddress(address);
    setPopularity(popularity);
    mIsFavorite = isFavorite;
    mIsVisible = isVisible;
    mCtx = ctx;
}

应用代码:

public class FragmentBaseGridActivity extends AbsListViewBaseActivity {

private ArrayList<Theme> mThemes;

private void refreshGrid() {
    mThemes = mComparator.compare(mSortType, mSortAorD, true, mThemes);
    ImageAdapter ad = (ImageAdapter) listView.getAdapter();
    ad.notifyDataSetChanged(); 
    }

public class Theme extends BaseTheme{

public String mStyle;

public Theme(int themeID, String name, String creator, String date, String style, String address, Boolean favorite, 
        Boolean visible, int popularity, Context ctx) {
    super(themeID, name, creator, date, address, favorite, visible, popularity, favorite, visible, ctx);

}

0 个答案:

没有答案