If I take the results given from an AsynkTask class from one another like this:
Register.java:
public void onClick(View v) {
LoadFermate backgroundFermate = new LoadFermate(Register.this);
ArrayList<HashMap<String, String>> fermate;
try{
fermate = backgroundFermate.execute().get();
for (HashMap<String, String> fermata:fermate){
Toast.makeText(Register.this , fermata.get("Nome"), Toast.LENGTH_SHORT).show();
}
}catch (InterruptedException e){
e.printStackTrace();
}catch (ExecutionException e){
e.printStackTrace();
}
}
the ProgressDialog is not shown. If instead I do all this stuff in the onPostExecute
method, it is shown. How is that possible?
I learned the way for getting the value from the othe class in that question.
Here I post the class where I use the ProgressDialog:
LoadingFermate.java:
package com.PacchettoPrincipale.app;
import [...]
public class LoadFermate extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
private Context context;
public LoadFermate(Context context){
this.context = context;
}
private ProgressDialog pDialog;
private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mCommentList;
//testing on Emulator:
private static final String DOWNLOAD_FERMATE_URL = "http://10.0.2.2/PrimaAppAndroid/fermate.php";
//JSON IDS:
private static final String TAG_COD = "CodFermata";
private static final String TAG_POSTS = "posts";
private static final String TAG_NOME = "Nome";
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context); //HERE I START THE PDIALOG, and in the onPostExecute method I dismiss it
pDialog.setMessage("Download fermate...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... voids) {
updateJSONData();
return mCommentList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> hashMaps) {
super.onPostExecute(hashMaps);
pDialog.dismiss();
}
private void updateJSONData() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(DOWNLOAD_FERMATE_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
int CodFermata = c.getInt(TAG_COD);
String Nome = c.getString(TAG_NOME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_COD, String.valueOf(CodFermata));
map.put(TAG_NOME, Nome);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}