在我的应用程序中,我通常有很多异步任务。它们看起来都一样,因此有着相同的设计。所以我想进一步优化一下,并创建一个通用的myasynctask,从中扩展我的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,它包含上面显示的所有内容,但以下内容除外:
方法做背景
onpostexecute方法
第二个构造器
... 以及asynctask本身的签名(第三个参数)
必须从特定的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;
}
}
}
下面是一个类扩展这个抽象类的想法。这两个错误写在代码中:
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);
}
}
2条答案
按热度按时间bnlyeluc1#
你可以创造一个
Abstract Class
. 这将允许您实现所有方法,并将依赖于任务的方法保留为抽象方法。这将允许任何人扩展您自己的(抽象)类,并强制他们实现这些特定的方法。u0njafvf2#
您可以创建一个抽象基类,其中包含两个泛型类型参数-r-表示结果类型,t表示搜索参数。
然后您可以使用以下特定实现扩展此类: