- Forum-Beiträge: 694
24.08.2012, 17:37:58 via Website
24.08.2012 17:37:58 via Website
Want to create a generic AsyncTask - how?
Die abstrakte Klasse habe ich mittlerweile mit etwas Hilfe zustande gebracht - aber eine Klasse die von dieser ableiten soll scheitert kläglich.
Danke.
Hier meine momentane abstrakte Klasse:
2
3 protected Context context;
4 protected MyProgressDialog dialog;
5 protected OnAsyncTaskCompletedListener listener;
6 protected int task_id;
7
8 public MyAsyncTask(int task_id, Context context, OnAsyncTaskCompletedListener listener) {
9 super();
10
11 this.context = context;
12 this.listener = listener;
13 this.task_id = task_id;
14 }
15
16 protected void attach(Context context, OnAsyncTaskCompletedListener listener) {
17 this.context = context;
18 this.listener = listener;
19
20 processCreateDialog();
21 }
22
23 protected void detach() {
24 processDismissDialog();
25
26 if (context != null) {
27 context = null;
28 }
29
30 if (listener != null) {
31 listener = null;
32 }
33 }
34
35 @Override
36 protected void onPostExecute(C result) {
37 if (listener != null) {
38 listener.onAsyncTaskCompleted(task_id, result);
39 }
40
41 detach();
42 }
43
44 @Override
45 protected void onPreExecute() {
46 processCreateDialog();
47 }
48
49 private void processCreateDialog() {
50 if (context != null) {
51 dialog = MyProgressDialog.show(context, null, null, true, false);
52 }
53 }
54
55 private void processDismissDialog() {
56 if (dialog != null) {
57 try {
58 dialog.dismiss();
59 } catch (Exception exception) {
60 }
61
62 dialog = null;
63 }
64 }
65}
Und hier wie ich mir eine Klasse vorstelle die diese erweitert - das klappt aber nicht:
2 // --> Fehler: MyAsyncTask cannot be resolved to a type - Create class 'MyAsyncTask<T1, T2, T3>'
3
4 private String search;
5
6 public MyAsyncTaskImpl(int task_id, Context context, OnAsyncTaskCompletedListener listener, String search) {
7 super(task_id, context, listener);
8
9 this.search = search;
10 }
11
12 @Override
13 protected Cursor doInBackground(final Void... voids) {
14 // --> Fehler: The method doInBackground(Void...) of type MyAsyncTaskImpl must override or implement a supertype method - Remove '@Override' annotation
15 return MyApplication.getSqliteOpenHelper().fetchSomething(search);
16 }
17}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.