1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
class ListEntriesActivity extends Activity
{
public void onCreate()
{
...
startDataLoading();
}
public void onDataLoaded(DATA d)
{
... refresh UI ...
}
public void onDataLoadError(String error)
{
... show error ? ....
}
public void onDestroy()
{
cancelDataLoading();
}
public void cancelDataLoading()
{
if (this.dataLoader != null)
this.dataLoader.cancel();
}
public void startDataLoading()
{
if (this.dataLoader == null) {
showProgress();
this.dataLoader = new DataLoadingTask();
this.dataLoader.execute();
}
}
public void showProgress(int v)
{
// display a progress icon somewhere to notify background loading
}
public void hideProgress()
{
// hide the progress icon
}
public class DataLoadingTask extends AsyncTask<?,DATA,?>
{
String error;
public void onPreExecute()
{
ListEntriesActivity.this.showProgress(0);
}
public DATA doInBackground()
{
DATA d = ..... ;
try {
// loading of the data
} catch (Exception ex) {
this.error = ....
}
return d;
}
public void onPostExecute(DATA d)
{
ListEntriesActivity.this.dataLoader = null;
if (d != null)
ListEntriesActivity.this.onDataLoaded(d);
else if (this.error != null)
ListEntriesActivity.this.onDataLoadError(this.error);
ListEntriesActivity.this.hideProgress();
}
}
} |
Partager