进度对话框未在Android中显示

时间:2014-08-07 12:45:33

标签: android android-asynctask progressdialog

进度对话框未在Android中的AsyncTask类中显示。

我的AsyncTask类代码:

class SyncData1 {

         Context context;
         DatabaseHelper myDbHelper;
         ArrayList<String> ItemNo= new  ArrayList<String>();
         ArrayList<String> RepReceivedQty= new  ArrayList<String>();
         ArrayList<String> HotelReceivedQty= new  ArrayList<String>();
         ArrayList<String> IStatus= new  ArrayList<String>();
          String jsodata;
         String ChallanNo="",HotelRepReceivedOn="",HotelReceivedOn="",Status="";
         String url=null;
         Commans com;
    public SyncData1(Context context)
    {
        this.context=context;
    }



      public void fetchData()
       {
           ItemNo.clear();
           IStatus.clear();
           RepReceivedQty.clear();
           HotelReceivedQty.clear();
            com= new Commans(context);
           myDbHelper =new DatabaseHelper(context);
           com.connectdb(myDbHelper);
           Cursor curval= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
           if(curval != null && curval.moveToFirst()) {

                     ChallanNo= curval.getString(0);
                     Status=curval.getString(5);
                     HotelRepReceivedOn=curval.getString(6);
                     HotelReceivedOn= curval.getString(7);

             }
           curval.close();
           Cursor curItem= myDbHelper.dbQery("Select * from ChallanItems where  ChallanNo= "+"'"+ChallanNo+"'");

           if(curItem != null && curItem.moveToFirst()) {
                 while( !curItem.isAfterLast())
                 {
                     ItemNo.add(curItem.getString(0));
                     IStatus.add(curItem.getString(2));
                     RepReceivedQty.add(curItem.getString(9));
                     HotelReceivedQty.add(curItem.getString(1));

                     curItem.moveToNext();
                 }
             }
           curItem.close();

           if(ChallanNo.length()>1)
           {
           jsodata=com.reciveJson(ChallanNo, HotelRepReceivedOn, HotelReceivedOn, Status, ItemNo, RepReceivedQty, HotelReceivedQty, IStatus);
           Log.d("Json", jsodata);
           ReciveChallanAsyn task = new ReciveChallanAsyn();
           task.execute("");
           }

       }



      private class ReciveChallanAsyn extends AsyncTask<String, Void, String> {
             public static final int HTTP_TIMEOUT = 30 * 1000; 
             @Override
                protected String doInBackground(String... urls) {
                  String response = "";
                  HttpEntity resEntity;
                    try {
                        //url=urls[0].getUrl();
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(Constants.SERVICE_SYNC_DATA);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                        nameValuePairs.add(new BasicNameValuePair("data",jsodata));
                        nameValuePairs.add(new BasicNameValuePair("token",CreateChallan.token));
                        nameValuePairs.add(new BasicNameValuePair("customer_code",CreateChallan.strcustomer_code));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        // Execute HTTP Post Request
                        HttpResponse responsePOST = httpclient.execute(httppost);
                         resEntity = responsePOST.getEntity();
                         response=EntityUtils.toString(resEntity);
                    } catch (Exception e) {
                      e.printStackTrace();
                    }

                  return response;
                }

                @SuppressLint("DefaultLocale")
                @Override
                protected void onPostExecute(String result) {
                    Log.d("Result", result);
                    pd.dismiss();
                    if(result!=null)
                    {

                       if(result.contentEquals("1") && result!=null)
                       {   com.connectdb(myDbHelper);
                           Toast.makeText(context, " Challan Sent ", Toast.LENGTH_LONG).show(); 
                           myDbHelper.ChallN_Updateb("", "2", ChallanNo,"");

                           Cursor curval1= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
                           if(curval1 != null && curval1.moveToFirst()) {

                              fetchData();
                           }
                           curval1.close();
                           com.disconnectdb();
                       }
                       else
                           Toast.makeText(context, "Error In Sending Challan", Toast.LENGTH_LONG).show(); 
                    }
                    else {
                        Toast.makeText(context, "Data Not Fount", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                protected  void onPreExecute()
                {
                //  pd = ProgressDialog.show(HomeChallan.this, "","  Please wait...");
                    pd = new ProgressDialog(HomeChallan.this);
                    pd.setMessage("  Please wait... ");
                    pd.setIndeterminate(true);
                    pd.setCancelable(false);
                    pd.show();

                }
              }

它是我的主类的子类,我在使用Async Task类

我在onclickListerner方法

上使用类SyncData1类
 Cursor curval1= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
                       if(curval1 != null && curval1.moveToFirst()) {
                           SyncData1 data= new SyncData1(context);
                            data.fetchData();

2 个答案:

答案 0 :(得分:0)

希望这可以解决您的问题,根据您的要求进行一些编辑..

 private final Handler handler = new Handler();

    private Thread thread;

    private QueueRunner runner = new QueueRunner();

    private CustomProgressDialog mProgressBar;

     private class QueueRunner implements Runnable 
     {
            public void run() 
            {
                try
                {
                    synchronized(this) 
                    {
                        handler.post(new Runnable() 
                        {
                            public void run() 
                            {
                                 if (((Activity) AsyncWebPostClient.this.context).isFinishing() == false)
                                     mProgressBar = CustomProgressDialog.show(AsyncWebPostClient.this.context, 
                                             null,null,true,true );
                            }
                    });
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
        }

     }
在你的preExecute中调用这个..

thread = new Thread(runner);
thread.start();

在你的onpost上打电话给这个..

mProgressBar.dismiss(); 

编辑:我使用的是customeProgress对话框,您可以使用进度对话框

答案 1 :(得分:0)

这是你的asynctask方法,复制并粘贴它希望它有效:

 private class ReciveChallanAsyn extends AsyncTask<String, Void, String> {
         public static final int HTTP_TIMEOUT = 30 * 1000; 
         @Override
         protected  void onPreExecute()
         {
         //  pd = ProgressDialog.show(HomeChallan.this, "","  Please wait...");
             pd = new ProgressDialog(HomeChallan.this);
             pd.setMessage("  Please wait... ");
             pd.setIndeterminate(false);
             pd.setCancelable(false);
             pd.show();

         }
         @Override
            protected String doInBackground(String... urls) {
              String response = "";
              HttpEntity resEntity;
                try {
                    //url=urls[0].getUrl();
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(Constants.SERVICE_SYNC_DATA);

                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("data",jsodata));
                    nameValuePairs.add(new BasicNameValuePair("token",CreateChallan.token));
                    nameValuePairs.add(new BasicNameValuePair("customer_code",CreateChallan.strcustomer_code));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    HttpResponse responsePOST = httpclient.execute(httppost);
                     resEntity = responsePOST.getEntity();
                     response=EntityUtils.toString(resEntity);
                } catch (Exception e) {
                  e.printStackTrace();
                }

              return response;
            }

            @SuppressLint("DefaultLocale")
            @Override
            protected void onPostExecute(String result) {
                Log.d("Result", result);
                pd.dismiss();
                if(result!=null)
                {

                   if(result.contentEquals("1") && result!=null)
                   {   com.connectdb(myDbHelper);
                       Toast.makeText(context, " Challan Sent ", Toast.LENGTH_LONG).show(); 
                       myDbHelper.ChallN_Updateb("", "2", ChallanNo,"");

                       Cursor curval1= myDbHelper.dbQery("Select * from Challan_R where Flag=1 limit 1");
                       if(curval1 != null && curval1.moveToFirst()) {

                          fetchData();
                       }
                       curval1.close();
                       com.disconnectdb();
                   }
                   else
                       Toast.makeText(context, "Error In Sending Challan", Toast.LENGTH_LONG).show(); 
                }
                else {
                    Toast.makeText(context, "Data Not Fount", Toast.LENGTH_LONG).show();
                }
            }


          }