开发者

Progress dialog is not displayed properly while navigating between tabs in android using AsyncTask

I hv 2 tabs in my application. when ever i click the 2nd tab i need to get the data from the server for tat i am using AsyncTask , every thing is working fine but the problem is with the progress dialog ie when i click on 2nd tab i need to show the progress dialog in 1 st tab ( Since 2nd tab is fetching the data from server call ) But progress dialog is displaying in 2nd tab which is not visible for me until server call finishes.

Can any one help me on this , Thanxs a lot..

AsyncTask code

 package com.cmc.bizgateway.net;
 import java.util.List;
 import org.apache.http.message.BasicNameValuePair;

 import android.app.ProgressDialog;
 import android.content.Context;
 import android.os.AsyncTask;
 import android.util.Log;
 import android.widget.Toast;

 public class ProgressBarManager extends
    AsyncTask<List<BasicNameValuePair>, St开发者_运维知识库ring, String> {

List list1 = null;
  Context context;
private ProgressDialog dialog = null;

public ProgressBarManager() {
    super();

}
public ProgressBarManager(Context context) {
    super();

}

public void setProgressDialog(Context c) {
    dialog = new ProgressDialog(c);
    this.context = c;
}

private NetworkManager networkManager = new NetworkManager();

@Override
protected String doInBackground(List... arg0) {
    // Log.v("in dobackground", "result");
    String response = null;
    try{
          response = networkManager.postWebService(arg0[0]);

    }catch(Exception e) {
        Log.v("", "exception in background " + e);
        Toast.makeText(context.getApplicationContext()," resId",4000).show();
    }
    return response;
}

protected void onPreExecute() {
    // Log.v("in pre execute", "result");
    this.dialog.setMessage("Loading. Please wait...");
    this.dialog.show();
}

/**
 * method is called after the work is done.
 * 
 * @param success
 *            result of #doInBackground method.
 */
// @Override
protected void onPostExecute(String success) {
    // Log.v("in post execute", "result");
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
}
 }

In tabhost class i am writing code

 secondTabSpec.setIndicator("Categories").setContent(
            new Intent(this, ProductCategoriesActivity.class));

and in ProductCategoriesActivity class

oncreate method i am using

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    ProgressBarManager p = new ProgressBarManager();
    p.setProgressDialog(context);

    List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(
            3);
    nameValuePairs.add(new BasicNameValuePair("method", "\""
            + "getCategoryList" + "\""));
    nameValuePairs.add(new BasicNameValuePair("type", "\"" + "product"
            + "\""));

    String result;
    try {
        result = p.execute(nameValuePairs).get();
                }


  ProgressDialog dialog; 

  dialog = new ProgressDialog(activity);
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.setMessage("Please Wait");
  public class background extends AsyncTask<String, Void, Void> 
  {

      @Override
      protected void onPreExecute() {
      // Log.v("in pre execute", "result");
      this.dialog.show();
  }

  protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

     return null;
  }

  protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
   if(dialog!=null)
        {
            dialog.dismiss();
        }

    }
   }

u may try this


Please do not use AsyncTask in order to display the progressbar.

what you should do is

create a new thread for doing your server operations & befaore startin that thread create &

show the progressbar, here is the sample code for doing this

final ProgressDialog dialog = ProgressDialog.show                   (this,getResources().getText(R.string.progressbartitle),              getResources().getText(R.string.progressbartext),true,false);

new Thread()
{
    public void run()
    {
         //do you web server call
    }
}.start();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜