如何在android中的post执行中关闭此进度对话框

时间:2014-05-06 09:13:44

标签: android android-asynctask progressdialog

我已经制作了自定义进度对话框,我想在我的postExecute方法中关闭它,我尝试过如下,但我不知道如何解雇它。我试过如下,任何人都可以告诉我怎么能解雇那个自定义对话框

主要

    package com.epe.yehki.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epe.yehki.adapter.ProductAdapter;
import com.epe.yehki.backend.BackendAPIService;
import com.epe.yehki.uc.Header;
import com.epe.yehki.uc.Menu;
import com.epe.yehki.util.Const;
import com.epe.yehki.util.Pref;
import com.epe.yehki.util.Utils;
import com.example.yehki.R;

public class SearchActivity extends Activity {
    public com.epe.yehki.uc.Menu searchMenu;
    public Header searchHeader;
    public EditText et_serach;
    JSONObject jsonObj;
    private ProgressDialog pDialog;
    Intent in = null;
    String searchUrl;
    int flag;
    public Header header;
    public Menu menu;
    public TextView title;
    Bitmap bitmap;;
    private ProductAdapter productContent;

    // PRODUCTS....
    // arrayLists......
    public static ArrayList<String> productArray;
    public static ArrayList<String> categoryArray;
    ProgressBar progressBar;

    //

    // contacts JSONArray
    JSONArray subcategories = null;
    JSONArray products = null;
    public String catid;
    public String id;
    public String pid;
    String name;
    ListView lv;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> subcategoryList;
    ArrayList<HashMap<String, String>> productList;

    // new
    public String proname;
    public String prodesc;
    public String proimg;

    public String proMinOrderQty;
    public String proMinPrice;
    public String proMaxPrice;
    public String proTerms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_search);
        productList = new ArrayList<HashMap<String, String>>();
        searchMenu = (com.epe.yehki.uc.Menu) findViewById(R.id.menusearch);
        searchMenu.setSelectedTab(2);
        searchHeader = (Header) findViewById(R.id.headersearch);
        searchHeader.title.setText("Search");
        et_serach = (EditText) findViewById(R.id.et_serach);
        lv = (ListView) findViewById(R.id.serch_list);
        productContent = new ProductAdapter(SearchActivity.this, productList);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (Pref.getValue(SearchActivity.this, Const.PREF_CUSTOMER_ID, "") != null && !Pref.getValue(SearchActivity.this, Const.PREF_CUSTOMER_ID, "").equals(" ")
                        && !Pref.getValue(SearchActivity.this, Const.PREF_CUSTOMER_ID, "").equals("0")) {
                    in = new Intent(getApplicationContext(), ProductDetailActivity.class);
                    proname = ((TextView) view.findViewById(R.id.product_label)).getText().toString();

                    // getting ProductId from the tag...

                    pid = productList.get(position).get(Const.TAG_PRODUCT_ID);
                    proimg = productList.get(position).get(Const.TAG_PRODUCT_IMG);
                    System.out.println(":::::::::::::::;;THE INTENT FOR THE PRODUCUT DETIALS ACTIVITY=================" + pid);

                    in.putExtra(Const.TAG_PRODUCT_ID, pid);
                    in.putExtra(Const.TAG_PRODUCT_IMG, proimg);
                    in.putExtra(Const.TAG_PRODUCT_NAME, proname);

                    startActivity(in);
                } else {
                    in = new Intent(SearchActivity.this, LoginActivity.class);
                    startActivity(in);
                }
            }
        });

        et_serach.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                System.out.println(":::::::::::::::;after text changed called:::");
                productList.clear();
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() != 0) {
                    System.out.println(":::::::::::::::;on text changed called:::");
                    // CALL SEARCH API........!!!
                    productList.clear();
                    new GetSearchList().execute();
                }

            }
        });
    }

    public void addProgressBar(Activity activity) {
        final ViewGroup rootFrameLayout = (ViewGroup) activity.getWindow().peekDecorView();
        final ViewGroup modal = new RelativeLayout(activity);
        progressBar = new ProgressBar(activity);
        LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ((android.widget.RelativeLayout.LayoutParams) layoutParams).addRule(RelativeLayout.CENTER_IN_PARENT);
        modal.addView(progressBar, layoutParams);
        rootFrameLayout.addView(modal, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        rootFrameLayout.invalidate();

    }

    // SEARCH API()..!!!
    private class GetSearchList extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            productList.clear();

            addProgressBar(SearchActivity.this);// pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            BackendAPIService sh = new BackendAPIService();
            searchUrl = Const.API_PRODUCT + "?product_name=" + et_serach.getText().toString().trim();
            System.out.println(":::::::::::::::::::SUB URL:::::::::::::::::" + searchUrl);
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(searchUrl, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);
                    if (jsonObj.getString(Const.TAG_STATUS).equals("success")) {
                        if (jsonObj.has(Const.TAG_PRODUCT_DETAIL)) {
                            System.out.println("::::::::::::::::true::::::::::::::::" + jsonObj.has(Const.TAG_PRODUCT_DETAIL));
                            products = jsonObj.getJSONArray(Const.TAG_PRODUCT_DETAIL);

                            if (products != null && products.length() != 0) {
                                // looping through All Contacts

                                for (int i = 0; i < products.length(); i++) {
                                    JSONObject c = products.getJSONObject(i);

                                    pid = c.getString(Const.TAG_PRODUCT_ID);
                                    System.out.println("::::::::::::::::::PARSING PRODUCT ID:::::::::::::" + pid);
                                    String proname = c.getString(Const.TAG_PRODUCT_NAME);
                                    String prodesc = c.getString(Const.TAG_LISTING_DESCRIPTION);
                                    String proimg = Const.API_HOST + "/" + c.getString(Const.TAG_PRODUCT_IMG);
                                    System.out.println("::::::::::::::;products Length:::::::::::" + products.length());
                                    System.out.println(":::::::::::::::My Image Url:::::::::::::" + proimg);
                                    String proMinOrderQty = c.getString(Const.TAG_PRODUCT_MIN_ORDER_QTY);
                                    c.getString(Const.TAG_PRODUCT_MIN_PRICE);
                                    c.getString(Const.TAG_PRODUCT_MAX_PRICE);

                                    c.getString(Const.TAG_PRODUCT_PAYMENT_TERMS);

                                    // for company details..!!!

                                    // new Working

                                    HashMap<String, String> product = new HashMap<String, String>();

                                    product.put(Const.TAG_PRODUCT_ID, pid);
                                    product.put(Const.TAG_PRODUCT_NAME, proname);
                                    product.put(Const.TAG_PRODUCT_IMG, proimg);
                                    product.put(Const.TAG_PRODUCT_MIN_ORDER_QTY, proMinOrderQty);
                                    product.put(Const.TAG_PRODUCT_DESCRIPTION, prodesc);
                                    productList.add(product);

                                }
                            }
                        }
                    } else {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                Utils.showCustomeAlertValidation(SearchActivity.this, "No Product found", "Yehki", "Ok");
                            }
                        });
                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println("::::::::::::::::::got an error::::::::::::");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog

            productContent.notifyDataSetChanged();
            lv.setAdapter(productContent);

        }

    }
}

4 个答案:

答案 0 :(得分:0)

简单易用: pDialog.dismiss();

答案 1 :(得分:0)

第1步

在创建

上面声明进度对话框
  

私人ProgressDialog pDialog;

第2步

定义preExecute方法

    protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(context);
    pDialog.setTitle("Please wait");
    pDialog.setMessage("Processing...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);


    pDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    pDialog.show();
}

第3步

//Your back ground task as it is

第4步

添加postExecute方法

 protected void onPostExecute()

       {
    pDialog.dismiss();

    }

答案 2 :(得分:0)

您只需为dismiss()调用ProgressDialog方法即可。像

 protected void onPostExecute(){

     pDialog.dismiss();

 }

答案 3 :(得分:0)

您拥有progressBar变量,该变量包含对布局中进度条的引用。要隐藏它,请致电

progressBar.setVisibility(View.GONE);

RelativeLayout放置ProgressBar是不必要的。另请考虑在您的布局中静态包含ProgressBar,并根据需要将其展示设置为VISIBLE / GONE