在 doInBackground 中更改通用 return 类型
Change the generic return type in doInBackground
下午好,我有一个完美运行的通用 baseAsyncTask,java 可以在 doInBackground 中定义类型,但在 onPostExecute 中我调用我的函数 doAsync,它组装为对象,就像我在创建 Override 时一样定义类型,这样我就不会 Cast
Class DaoProcessorAsync
public abstract class DaoAsyncProcessor<T> {
public interface DaoProcessCallback<T>{
void onResult(T result);
}
private final DaoProcessCallback daoProcessCallback;
public DaoAsyncProcessor(DaoProcessCallback daoProcessCallback) {
this.daoProcessCallback = daoProcessCallback;
}
protected abstract T doAsync();
public void start(){
new DaoProcessAsyncTask().execute();
}
private class DaoProcessAsyncTask extends AsyncTask<Void, Void, T> {
@Override
protected T doInBackground(Void... params) {
return doAsync();
}
@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(T t) {
if(daoProcessCallback != null)
daoProcessCallback.onResult(t);
}
}
}
致电class
new DaoAsyncProcessor<Trabalhador>(new DaoAsyncProcessor.DaoProcessCallback() {
@Override
// How to make it explicit that the return is of the Worker type
public void onResult(Object result) {
**at that point it returns object to me, and I need to do conversion**
Trabalhador trabalhador = (Trabalhador) result;
}
}){
@Override
protected Trabalhador doAsync() {
**This worker-type return should be explicit in OnRESULT, but I can't define it**
return trabalhadorDao.lerPorCPF(123);
}
}.start();
我认为您在创建 DaoProcessCallback 时缺少类型。
尝试
new DaoAsyncProcessor<Tabalhador>.DaoProcessCallback()
下午好,我有一个完美运行的通用 baseAsyncTask,java 可以在 doInBackground 中定义类型,但在 onPostExecute 中我调用我的函数 doAsync,它组装为对象,就像我在创建 Override 时一样定义类型,这样我就不会 Cast
Class DaoProcessorAsync
public abstract class DaoAsyncProcessor<T> {
public interface DaoProcessCallback<T>{
void onResult(T result);
}
private final DaoProcessCallback daoProcessCallback;
public DaoAsyncProcessor(DaoProcessCallback daoProcessCallback) {
this.daoProcessCallback = daoProcessCallback;
}
protected abstract T doAsync();
public void start(){
new DaoProcessAsyncTask().execute();
}
private class DaoProcessAsyncTask extends AsyncTask<Void, Void, T> {
@Override
protected T doInBackground(Void... params) {
return doAsync();
}
@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(T t) {
if(daoProcessCallback != null)
daoProcessCallback.onResult(t);
}
}
}
致电class
new DaoAsyncProcessor<Trabalhador>(new DaoAsyncProcessor.DaoProcessCallback() {
@Override
// How to make it explicit that the return is of the Worker type
public void onResult(Object result) {
**at that point it returns object to me, and I need to do conversion**
Trabalhador trabalhador = (Trabalhador) result;
}
}){
@Override
protected Trabalhador doAsync() {
**This worker-type return should be explicit in OnRESULT, but I can't define it**
return trabalhadorDao.lerPorCPF(123);
}
}.start();
我认为您在创建 DaoProcessCallback 时缺少类型。
尝试
new DaoAsyncProcessor<Tabalhador>.DaoProcessCallback()