在我的应用程序中,我通常有很多AsyncTasks。它们看起来都一样,因此共享相同的设计。所以我想进一步优化它,并创建一个通用的MyAsyncTask,我的AsyncTasks可以从中扩展。但我不知道该怎么做。
这是我的AsyncTasks之一。它们的区别仅在于使用 * 包围的方法/变量:
public class OneOfMyAsyncTasks extends AsyncTask<Void, Void, ***Cursor***> {
private Context context;
private MyProgressDialog dialog;
private OnAsyncTaskCompletedListener listener;
private String search;
private int task_id;
public OneOfMyAsyncTasks(int task_id, Context context, OnAsyncTaskCompletedListener listener) {
super();
this.context = context;
this.listener = listener;
this.task_id = task_id;
}
public OneOfMyAsyncTasks(int task_id, Context context, OnAsyncTaskCompletedListener,
***String search***) {
super(task_id, context, listener);
***this.search = search;***
}
protected void attach(Context context, OnAsyncTaskCompletedListener listener) {
this.context = context;
this.listener = listener;
processCreateDialog();
}
protected void detach() {
processDismissDialog();
if (context != null) {
context = null;
}
if (listener != null) {
listener = null;
}
}
@Override
protected ***Cursor*** doInBackground(Void... voids) {
***return MyApplication.getSqliteOpenHelper().fetchSomething(search);***
}
@Override
protected void onPostExecute(***Cursor cursor***) {
if (listener != null) {
listener.onAsyncTaskCompleted(task_id, ***cursor***);
}
detach();
}
@Override
protected void onPreExecute() {
processCreateDialog();
}
private void processCreateDialog() {
if (context != null) {
dialog = MyProgressDialog.show(context, null, null, true, false);
}
}
private void processDismissDialog() {
if (dialog != null) {
try {
dialog.dismiss();
} catch (Exception exception) {
}
dialog = null;
}
}
}
我的想法是创建一个MyAsyncTask,它包含上面显示的所有内容,但以下情况除外:
这些必须从特定的AsyncTasks设置,这些AsyncTasks从这个通用的MyAsyncTask扩展。
非常感谢任何帮助。
编辑:这是我迄今为止所做的 - 没有成功。扩展类抛出两个错误,这些错误写在下面的代码中。首先是抽象类:
public abstract class MyAsyncTask<A, B, C> extends AsyncTask<A, B, C> {
protected Context context;
protected MyProgressDialog dialog;
protected OnAsyncTaskCompletedListener listener;
protected int task_id;
public MyAsyncTask(int task_id, Context context, OnAsyncTaskCompletedListener listener) {
super();
this.context = context;
this.listener = listener;
this.task_id = task_id;
}
protected void attach(Context context, OnAsyncTaskCompletedListener listener) {
this.context = context;
this.listener = listener;
processCreateDialog();
}
protected void detach() {
processDismissDialog();
if (context != null) {
context = null;
}
if (listener != null) {
listener = null;
}
}
@Override
protected void onPostExecute(C result) {
if (listener != null) {
listener.onAsyncTaskCompleted(task_id, result);
}
detach();
}
@Override
protected void onPreExecute() {
processCreateDialog();
}
private void processCreateDialog() {
if (context != null) {
dialog = MyProgressDialog.show(context, null, null, true, false);
}
}
private void processDismissDialog() {
if (dialog != null) {
try {
dialog.dismiss();
} catch (Exception exception) {
}
dialog = null;
}
}
}
这就是扩展Abstract类的类的想法。这两个错误写在代码中:
public class MyAsyncTaskImpl extends MyAsyncTask<Void, Void, Cursor> {
// --> Error: MyAsyncTask cannot be resolved to a type - Create class 'MyAsyncTask<T1, T2, T3>'
private String search;
public MyAsyncTaskImpl(int task_id, Context context, OnAsyncTaskCompletedListener listener, String search) {
super(task_id, context, listener);
this.search = search;
}
@Override
protected Cursor doInBackground(Void... voids) {
// --> Error: The method doInBackground(Void...) of type MyAsyncTaskImpl must override or implement a supertype method - Remove '@Override' annotation
return MyApplication.getSqliteOpenHelper().fetchSomething(search);
}
}
答案 0 :(得分:2)
您可以使用两个泛型类型参数创建一个抽象基类 - R - 表示结果类型,T表示搜索参数。
public abstract class MyAsyncTask<R, T> extends AsyncTask<Void, Void, R> {
private Context context;
private MyProgressDialog dialog;
private OnAsyncTaskCompletedListener listener;
private T search;
private int task_id;
public MyAsyncTask(int task_id, Context context,
OnAsyncTaskCompletedListener listener) {
super();
this.context = context;
this.listener = listener;
this.task_id = task_id;
}
public MyAsyncTask(int task_id, Context context,
OnAsyncTaskCompletedListener listener, T search) {
this(task_id, context, listener);
this.search = search;
}
protected void attach(Context context, OnAsyncTaskCompletedListener listener) {
this.context = context;
this.listener = listener;
processCreateDialog();
}
protected void detach() {
processDismissDialog();
if (context != null) {
context = null;
}
if (listener != null) {
listener = null;
}
}
@Override
protected void onPostExecute(R result) {
if (listener != null) {
listener.onAsyncTaskCompleted(task_id, result);
}
detach();
}
@Override
protected void onPreExecute() {
processCreateDialog();
}
private void processCreateDialog() {
if (context != null) {
dialog = MyProgressDialog.show(context, null, null, true, false);
}
}
private void processDismissDialog() {
if (dialog != null) {
try {
dialog.dismiss();
} catch (Exception exception) {
}
dialog = null;
}
}
}
然后,您可以使用以下具体实现扩展此类:
public class MyAsyncTaskimpl extends MyAsyncTask<Cursor, String> {
public MyAsyncTaskimpl(int task_id, Context context,
OnAsyncTaskCompletedListener listener, String search) {
super(task_id, context, listener, search);
// TODO Auto-generated constructor stub
}
@Override
protected Cursor doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
答案 1 :(得分:0)
您可以创建Abstract Class
。这将允许您实现所有方法,并将与任务相关的方法保留为Abstract。这将允许任何人扩展您自己的(抽象)类并强制它们实现这些特定方法。