I\'m new to Android development and I\'m trying to code a little app which allows me to grab an external JSON file and parse it. I got this to work, however it wont work if
The implementation of AsyncTask
in one of the other answers is flawed. The progress dialog is being created every time within publishProgress
, and the reference to the dialog is not visible outside the method. Here is my attempt:
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new LongOperation().execute();
}
class LongOperation extends AsyncTask {
ProgressDialog pd = null;
TextView tv = null;
@Override
protected void onPreExecute(){
tv = Main.this.findViewById(R.id.textvewid);
pd = new ProgressDialog(Main.this);
pd.setMessage("Working...");
// setup rest of progress dialog
}
@Override
protected String doInBackground(String... params) {
//perform existing background task
return result;
}
@Override
protected void onPostExecute(String result){
pd.dismiss();
tv.setText(result);
}
}
}