在我的应用程序中,我通常有很多 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 ,它确实包含上面显示的所有内容,但以下内容除外:
- 方法doInBackground
- 方法 onPostExecute
- 第二个构造函数
- ...以及 AsyncTask 本身的签名(第三个参数)
这些必须从从这个通用 MyAsyncTask 扩展的特定 AsyncTask 中设置。
非常感谢任何帮助。
编辑:这是我到目前为止所做的 - 没有成功。扩展类抛出两个错误,这些错误写在下面的代码中。首先是抽象类:
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;
}
}
}
这是扩展该抽象类的类的想法。这两个错误写在代码中:
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);
}
}