简体   繁体   English

当涉及泛型时(特别是List),通过继承重用Java代码?

[英]Java code reuse through Inheritance when generics are involved (List in particular)?

I have very little knowledge of generics other than how to 'use them'. 除了如何“使用它们”外,我对泛型几乎一无所知。 In my app I have various ListView Activity that seem to share similar methods and variables and have the exact 'algorithm' in the sense that the steps performed are all very much the same. 在我的应用程序中,我有各种ListView活动,它们似乎共享相似的方法和变量,并且在执行步骤都非常相同的意义上具有确切的“算法”。

For example in my onCreate method I call a method to add a footer, start a progress dialog, call a method to get content based on a xml url and there is a Handler and Runnable fields that is used to call the method to fill the data for the list when the xml parsing is done. 例如,在我的onCreate方法中,我调用一种方法来添加页脚,启动进度对话框,调用一种方法以基于xml url获取内容,并且有一个Handler和Runnable字段,用于调用该方法来填充数据xml解析完成后的列表。

I was thinking maybe I can create a BaseListActivity that does all these things/has methods to all these things and each specific ListActivity can be extended from that. 我在想,也许我可以创建一个BaseListActivity来对所有这些事情执行所有操作/具有方法,并且可以从中扩展每个特定的ListActivity。 The problem is they use a List object to hold the items that the xml parsed and which the ListAdapter is backed by. 问题在于它们使用List对象来保存xml解析的项目以及支持ListAdapter的项目。

So here is code of the fields used: 因此,这是使用的字段的代码:

// list of games from xml
private List<Game> mGames = new ArrayList<Game>();
private List<Game> mNewGames = null;

// Need handler for callbacks to the UI thread
private final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        mGames.addAll(mNewGames);
        fillData();
    }
};

LayoutInflater mInflater = null;
private ProgressDialog mProgressDialog = null;
private int currentPage = 0;

so really the main difference is that each different activity would use a different object type for the List (ie. Game, Media, Article, etc.). 因此,真正的主要区别在于,每个不同的活动将为列表使用不同的对象类型(即游戏,媒体,文章等)。 How would I go about doing this? 我将如何去做呢?

I am thinking of something like: 我在想类似的东西:

public abstract class BaseListActivity<T> extends ListViewActivity {
    private List<T> mItems; 

    protected abstract List<T> readAllFromXML();
    ...

}

The parameter T stands for the actual type the list activity implementation works with. 参数T代表列表活动实现所使用的实际类型。 For example, a derived class can look like: 例如,派生类可以如下所示:

public TexyListActivity extends BaseListActivity<String> {
    protected List<String> readAllFromXML() {
        ....
    }
    ...
}

The method readAllFromXML is left for subclasses to implement, since each implementation instantiates different object types for the list, therefore it is using a different logic to create them from XML. readAllFromXML方法留给子类实现,因为每种实现都会实例化列表的不同对象类型,因此它使用不同的逻辑从XML创建它们。

Will an Adapter example do? 适配器示例可以吗?

public abstract class OnlineListAdapterTyped<T> extends BaseAdapter {

private final ArrayList<T> items = new ArrayList<T>();

    @Override
    public final T getItem(int position) {
        return items.get(position);
    }

    protected abstract T deserialize(JSONObject obj) throws JSONException;
}

public class CategoriesAdapter extends
        OnlineListAdapterTyped<CatalogAppCategory> {

    @Override
    protected CatalogApp deserialize(JSONObject obj) throws JSONException {
    //
    }
}

Define a templatized list 定义模板列表

List<E> mObjItems = new ArrayList(); 

and use that to hold classes like Game , Media , or Article . 并用来举办GameMediaArticle类的课程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM