Android : How to convert this inner AsyncTask in outer one?

on Monday, October 6, 2014


I created this AsyncTask inner class and now i want to make it outer. Here is the code:



private class DownloadDataFromServer extends AsyncTask<String, Integer, String> {
ProgressDialog dialog;
boolean connErr = false;
boolean soErr = false;

@Override
protected void onPreExecute() {
dialog = new ProgressDialog(HomePage.this); !!!!PROBLEM!!!!
dialog.setIndeterminate(false);
dialog.setMax(100);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
dialog.setTitle("Download");
dialog.setMessage("Updating...");
dialog.show();

super.onPreExecute();
}

@Override
protected String doInBackground(String... urls) {
// do something
}

protected void onProgressUpdate(Integer... progress) {
// Update the progress
dialog.setProgress(progress[0]);
}

@Override
protected void onPostExecute(String result) {
if (connErr || soErr) {
String msg = "Bad connection";
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(HomePage.this); !!!!PROBLEM!!!!
builder.setCancelable(false);
builder.setTitle("Connection timeout");
builder.setMessage(msg);

builder.setPositiveButton("Retry", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
new DownloadDataFromServer().execute(new String[] { "http://www.example.com" });
}
});

builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish(); !!!!PROBLEM!!!!
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
if (Integer.parseInt(result) <= 0) {
Toast.makeText(getBaseContext(), "Error!", Toast.LENGTH_SHORT).show(); !!!!PROBLEM!!!!
} else {
Toast.makeText(getBaseContext(), "OK!", Toast.LENGTH_SHORT).show(); !!!!PROBLEM!!!!
}
}
dialog.dismiss();
}
}


Please note "!!!!PROBLEM!!!!" strings in the above code: there are all the errors that i get. I tried to apply some workarounds but finally i could not make it working. In addition i also have to use



private Database db = new Database(this);


but i do not know what to put in place of "this". Please help me to convert this inner class in the right way.


0 comments:

Post a Comment