gpt4 book ai didi

java - 想要创建一个通用的 AsyncTask - 如何?

转载 作者:太空狗 更新时间:2023-10-29 13:34:30 24 4
gpt4 key购买 nike

在我的应用程序中,我通常有很多 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本身的签名(第三个参数)

这些必须从特定的 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);
}
}

最佳答案

您可以创建一个带有两个泛型类型参数的抽象基类 - 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;
}

}

关于java - 想要创建一个通用的 AsyncTask - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com