开发者

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.

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.

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. The problem is they use a List object to hold the items that the xml parsed and which the ListAdapter is backed by.

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(mN开发者_运维问答ewGames);
        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. 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜