J'ai un AsyncTask qui récupère les données du serveur. Ce dont j'ai besoin est de montrer les données récupérées dans un dialogue. Comment puis-je appeler la fonction pour créer la boîte de dialogue depuis l'AsyncTask? Si je ne peux pas faire cela, alors quelles options ai-je? Est-il possible de savoir si un AsyncTask a été terminé ou non?Est-il possible d'appeler une autre fumction depuis l'intérieur d'AsyncTask?
Voici mon code:
//Function calling the AsyncTask
public void getData(int pos)
{
new DBTask().execute();
//Is it possible to know here if the AsyncTask has ended or not??
}
private class DBTask extends AsyncTask<Long, Boolean, Integer>{
ProgressDialog ServerPD = new ProgressDialog(MRRankingActivity.this);
@Override
protected void onPreExecute()
{
ServerPD = ProgressDialog.show (MRRankingActivity.this, "", "Connecting to server...", true, false);
}//End of onPreExecute
@Override
protected Integer doInBackground(Long... params)
{
publishProgress(isOnline());
if(isOnline())
{
getDBData();
if(isOK)
{
//I want to show the result by calling this function..
showResult();
//Calling this function will trow error..So how can i show my result??
Log.d("LIST","DONE..Reached Final Destination");
}
}
}
return null;
}
@Override
protected void onProgressUpdate(Boolean... isConnection) {
// TODO Auto-generated method stub
super.onProgressUpdate(isConnection);
if(isOnline())
{
ServerPD.setMessage("Retreving Data");
}
}
@Override
protected void onPostExecute(Integer result) {
if (ServerPD.isShowing())
{
ServerPD.dismiss();
}
}//End of onPostExecute
}//End of DBTask
public void getDBData()
{
try{
//Code foe connecting to database and retreive data.
//If everything is ok then isOK is set to True..
//else its set to false
isOK=true;
}
catch(Exception e)
{
isOK=false;
}//End if catch
}
public void showResult()
{
//I want to show result here in a new Dialog ...
}
//Checking is connection available
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
Comment puis-je montrer le résultat dans cette situation? Existe-t-il une méthode pour déterminer si AsyncTask est terminé?
merci ... vous l'avez fait si simple – Shijilal