3

在我的应用程序中,我通常有很多 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);
    }
}
4

2 回答 2

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;
    }

}
于 2012-08-24T12:28:10.047 回答
0

你可以创建一个Abstract Class. 这将允许您实现所有方法并将依赖于任务的方法保留为抽象。这将允许任何人扩展您自己的(抽象)类并强制他们实现这些特定方法。

于 2012-08-24T12:10:57.233 回答