1

我正在使用以下功能在我的 android 视图中创建/显示加载

public void showDialogue(String Message, String Title){
    builder = new AlertDialog.Builder(this);
    progress = new ProgressBar(this);
    builder.setMessage(Message);
    builder.setView(progress);
    builder.create().show();

}

我将此函数称为异步任务

private class SetParm extends AsyncTask<String, Integer, String> {

        Integer myid;
        Integer myFlag;
        Integer removeId;
        @Override
        protected String doInBackground(String... sUrl) {
            try {

                SharedPreferences mPrefs = getSharedPreferences("prefs",0);    
                String restoredText = mPrefs.getString("access_token", ""); 
                String path = "http://www.sitename.com/app/setFlag.php";

                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout

                HttpResponse response;
                JSONObject json = new JSONObject();
                try {
                    HttpPost post = new HttpPost(path);
                    json.put("access_token", restoredText);
                    json.put("id", myid);
                    json.put("flag", myFlag);

                    Log.i("jason Object", json.toString());
                    post.setHeader("json", json.toString());
                    StringEntity se = new StringEntity(json.toString());
                    se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);
                    /* Checking response */
                    if (response != null) {
                        InputStream in = response.getEntity().getContent(); 

                        String a = convertStreamToString(in);

                        JSONObject jsono = stringToJsonobj(a);
                        Log.v("TAGG",a);
                        String passedStringValue = jsono.getString("result");


                        if(passedStringValue.equals("1")){
                            flags=1;
                            //Log.v("TAGG", "Success");
                            SharedPreferences mPrefss = getSharedPreferences("prefs", 0);    
                            SharedPreferences.Editor editor = mPrefss.edit();    
                            editor.putString("access_token", jsono.getString("access_token"));  
                            editor.commit();
                        }
                        else {
                            flags=0;
                            //Log.v("TAGG", "Failed !");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {
            }
            return null;
        }


        @Override
        protected void onPreExecute() {
            showDialogue("Regestring Your Devide... Please wait.", "Regestring Devide");

            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
        }



        @Override
        protected void onPostExecute(String result) {
            builder.cancel();
            if(flags.equals(1)){
                TableLayout mTable = (TableLayout)findViewById(R.id.tableLayout_1);
                mTable.removeView(findViewById(4500+removeId));
                mTable.removeView(findViewById(6500+removeId));

                int count = mTable.getChildCount();
                if(count<=0){
                    lists="";
                    total="0";
                    LogIN loginUsers1 = new LogIN();
                    loginUsers1.execute("");

                }

            }
            else {
                TextView text = (TextView) findViewById(R.id.status_msg);
                text.setText("Error while processing requests. Please try again.");
            }
            super.onPostExecute(result);
        }

    }

从 onPreExecute() 函数调用警报对话框。

现在我需要在 web 服务请求完成后删除加载

所以我写builder.cancel();onPostExecute 但它不起作用

任何想法 ?提前致谢

4

6 回答 6

3

而不是正常的dialog,您可以使用progress dialog如下所示:

private ProgressDialog progressDialog;

protected void onPreExecute() {
            super.onPreExecute();
            progressDialog= new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading ....");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            progressDialog.show();
        }

 protected void onPostExecute(String result) {
progressDialog.dismiss();
}
于 2013-05-17T11:35:03.963 回答
3

进度条自定义.....

         //Pre Execute method

            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
                ProgressDialog pDialog = new ProgressDialog(Activity.this);

                pDialog= ProgressDialog.show(.this, "","Loading.    Please wait...", true);
                pDialog.setContentView(R.layout.progressbar);

                pDialog.setCancelable(false);

            }

    ////////////////////////////////////////////////////////////////////////////////////////

        Progress bar xml layout
        that is progressbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:background="@drawable/progressbar_shap"
        android:layout_gravity="center">

      <TextView  android:id="@+id/textview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:text="Please Wait..."
       android:textStyle="bold"
      />
      <ProgressBar 
       android:indeterminateDrawable="@drawable/my_progress_indetermine"
       android:layout_height="60dp" 
       android:layout_width="60dp"
       android:layout_centerInParent="true"
       ></ProgressBar>
    </RelativeLayout>


  ////////////////////////////////////////////////////////////////////////
  After that 
  my_progressbar_indertimine.xml

    <?xml version="1.0" encoding="utf-8"?>
    <animated-rotate 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/please_wait"
     android:pivotX="50%"
     android:pivotY="50%" />





//Post execute method , which will dismiss progress bar.


@Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all albums
            pDialog.dismiss();
}

“please_wait”进度条图片,它会显示白色,当您使用下面的链接时,您可以保存它,右键单击浏览器页面并使用“另存为”

  http://i.stack.imgur.com/hnY8r.png
于 2013-05-17T12:16:29.620 回答
2

像这样试试

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Activity.this);
        pDialog.setMessage("Loading Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }   

在 onPostExecute 中这样做

     protected void onPostExecute(String result) {
        super.onPostExecute(toString());
        // dismiss the dialog after loading
        pDialog.dismiss();          
            }
于 2013-05-17T11:23:49.240 回答
1

您可以使用而Dialog不是AlertDialog调用dialog.dismiss()onPostExecute()

编辑:请不要将此答案视为解决方案。因为我认为 ramesh 正在使用AlertDialog.Builder,并且回答错误。与Dialog我们有更多自定义选项一样,我建议这样做。

于 2013-05-17T11:25:23.890 回答
0

试试这个代码的异步任务和进度对话框,

    public class MyTask extends AsyncTask<Void, Integer, Void> {

    public MyTask(Activity activity, int id) {
        this.activity = activity;


        context = activity;

    }

    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog progressDialog;
    /** application context. */
    private Activity activity;
    private Context context;


    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        progressDialog = ProgressDialog.show(context, "", "Loading...");



    }

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

                    return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
                    progressDialog.dismiss();

             }

}
于 2013-05-17T11:36:59.970 回答
0

在你的 onPostExecute 中试试这个.....

    @Override
    protected void onPostExecute(String result) {
        try{
        builder.dismiss();
         }
      catch(Exception e){

            }
        if(flags.equals(1)){
            TableLayout mTable = (TableLayout)findViewById(R.id.tableLayout_1);
            mTable.removeView(findViewById(4500+removeId));
            mTable.removeView(findViewById(6500+removeId));

            int count = mTable.getChildCount();
            if(count<=0){
                lists="";
                total="0";
                LogIN loginUsers1 = new LogIN();
                loginUsers1.execute("");

            }

        }
        else {
            TextView text = (TextView) findViewById(R.id.status_msg);
            text.setText("Error while processing requests. Please try again.");
        }

    }
于 2013-05-17T11:29:34.333 回答