繁体   English   中英

AsyncTask将通用类型传递给内部接口结果处理程序

[英]AsyncTask pass Generic Type to inner interface result handler

我如何在静态上下文中使用Type参数的AsyncTask子类(我打算再次对其进行子类化)中使用变量Type参数?

例如:

public abstract class ListAsyncTask<Identifier,ListType> extends AbsAsyncTask<Identifier, List<ListType>> {
protected final String TAG = Utils.generateTag(this.getClass());
ResultListener listener;

WebProxy proxy;
String server;

public interface ResultListener {
    void handleAsyncResult(List<?> result);
}

我正在使用ResultListener返回AsyncTask的结果,我想在ResultListener的接口方法签名中使用Type变量“ ListType”,但由于该接口的静态上下文作为内部接口,因此无法引用它。

我想我可以在AsyncTask类之外定义接口,但其想法是使它们紧密连接。

我认为您有2个选择。

(1)使接口通用:

public interface ResultListener<ListType> {
    void handleAsyncResult(List<ListType> result);
}

(2)使用(内部)抽象类而不是接口:

public abstract class ResultListener {
    void handleAsyncResult(List<ListType> result);
}

暂无
暂无

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

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