ProgressDialog.show()未显示进度对话框

时间:2016-11-07 11:22:15

标签: android progressdialog

我正在尝试在片段内显示进度对话框。但是progressbar.show()没有任何效果。但是,我注意到一个奇怪的行为,如果我两次调用showPopUp()方法,它会显示进度对话框,但无法解雇()。

package com.snapbizz.snapdashboard.Tabs.v1;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.snapbizz.snapdashboard.Core.v1.SalesData;
import com.snapbizz.snapdashboard.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class DashBoardSalesTab extends Fragment {

    LinearLayout salesListContainer, salesBarConatainer;
    LayoutInflater layoutInflater;
    SalesData salesData;
    ProgressDialog progressDialog;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.sales_page_layout, container, false);
        return rootView;
    }

    public void showPopUp() {
        progressDialog = new ProgressDialog(getContext(), ProgressDialog.THEME_HOLO_LIGHT);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("loading the page...");
        progressDialog.setProgressNumberFormat(null);
        progressDialog.setProgressPercentFormat(null);
        progressDialog.setIndeterminate(true);
        progressDialog.show();

    }

    public void synchronizeScrollers() {
        getActivity().findViewById(R.id.page_scroller).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                getActivity().findViewById(R.id.table_scroller).getParent()
                        .requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
        getActivity().findViewById(R.id.table_scroller).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
    }

    public void renderSalesGraph(List<String[]> values, Float totalSumY) throws Exception {

        salesBarConatainer.removeAllViews();
        for (String[] value : values) {
            View barView = layoutInflater.inflate(R.layout.bar_char_item_layout, salesBarConatainer, false);
            float sumOfSalesForTheDay = Float.parseFloat((value[0] == null) ? "0" : value[0]);
            float weightofBar = sumOfSalesForTheDay / totalSumY;
            barView.findViewById(R.id.bar_y).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, weightofBar));

            ((TextView) barView.findViewById(R.id.value_y)).setText(
                    (sumOfSalesForTheDay == 0.0f ? "" :
                            getActivity().getResources().getString(R.string.rupee_symbol) + sumOfSalesForTheDay + "")
            );
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy/MM/dd");
            SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MMM-yyyy");
            Date renderingDate = inputFormat.parse(value[2]);
            String[] date = outputFormat.format(renderingDate).split("-");
            ((TextView) barView.findViewById(R.id.value_x_date)).setText(date[0] + " " + date[1]);
            ((TextView) barView.findViewById(R.id.value_x_year)).setText(date[2]);
            salesBarConatainer.addView(barView);
        }
    }

    public void renderSalesGraphForMonths(List<String[]> values, Float totalSumY) throws Exception {
        LinearLayout salesBarConatainer = (LinearLayout) getActivity().findViewById(R.id.bars_container);
        salesBarConatainer.removeAllViews();
        for (String[] value : values) {
            View barView = layoutInflater.inflate(R.layout.bar_char_item_layout, salesBarConatainer, false);
            float sumOfSalesForTheDay = Float.parseFloat((value[0] == null) ? "0" : value[0]);
            float weightofBar = sumOfSalesForTheDay / totalSumY;
            barView.findViewById(R.id.bar_y).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, weightofBar));

            ((TextView) barView.findViewById(R.id.value_y)).setText(
                    (sumOfSalesForTheDay == 0.0f ? "" :
                            getActivity().getResources().getString(R.string.rupee_symbol) + sumOfSalesForTheDay + "")
            );
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy/MM");
            SimpleDateFormat outputFormat = new SimpleDateFormat("MMM-yyyy");
            Date renderingDate = inputFormat.parse(value[2]);
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MONTH, renderingDate.getMonth());
            cal.set(Calendar.DAY_OF_MONTH, 1);

            barView.setOnClickListener(new MonthBarClickListener(cal));
            String[] date = outputFormat.format(renderingDate).split("-");
            ((TextView) barView.findViewById(R.id.value_x_date)).setText(date[0]);
            ((TextView) barView.findViewById(R.id.value_x_year)).setText(date[1]);
            salesBarConatainer.addView(barView);
        }
    }

    public String[] addTOSalesTable(String date, boolean header) throws Exception {
        List<String[]> values = salesData.getSalesTableData(date);
        String[] value = values.get(0);
        String[] newValue = {value[0], value[1], date};
        String totalSales = (value[0] == null) ? "0" : value[0];
        String totalCredit = (value[1] == null) ? "0" : value[1];
        String totalCash = (Float.parseFloat(totalSales) - Float.parseFloat(totalCredit)) + "";
        String rupeeSymbol = getActivity().getResources().getString(R.string.rupee_symbol);
        if (!header) {
            View salesRow = layoutInflater.inflate(R.layout.sales_page_table_row_layout, salesListContainer, false);
            ((TextView) (salesRow.findViewById(R.id.sale_date))).
                    setText(date);
            ((TextView) (salesRow.findViewById(R.id.sales_total_sale))).
                    setText(rupeeSymbol + " " + totalSales);
            ((TextView) (salesRow.findViewById(R.id.sales_total_cash))).
                    setText(rupeeSymbol + " " + totalCash);
            ((TextView) (salesRow.findViewById(R.id.sales_total_credit))).
                    setText(rupeeSymbol + " " + totalCredit);
            ((TextView) (salesRow.findViewById(R.id.sales_ttoal_coupon))).
                    setText(rupeeSymbol + " " + "0");
            if (salesListContainer.getChildCount() % 2 != 0) {
                salesRow.setBackgroundColor(getResources().getColor(R.color.table_row_alternate_color));
            }
            salesListContainer.addView(salesRow);
        } else {
            ((TextView) (getActivity().findViewById(R.id.sales_header_total_sales))).
                    setText(rupeeSymbol + " " + totalSales);
            ((TextView) (getActivity().findViewById(R.id.sales_header_total_cash))).
                    setText(rupeeSymbol + " " + totalCash);
            ((TextView) (getActivity().findViewById(R.id.sales_header_total_credit))).
                    setText(rupeeSymbol + " " + totalCredit);
            ((TextView) (getActivity().findViewById(R.id.sales_header_total_coupon))).
                    setText(rupeeSymbol + " " + "0");
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            Date presentDate = new Date();
            ((TextView) (getActivity().findViewById(R.id.sales_header_day))).
                    setText(date.contentEquals(dateFormat.format(presentDate)) ? "Today" : date);
        }
        return newValue;
    }

    public void getMonths() throws Exception {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM");

        List<String[]> values = new ArrayList<>();
        float totalSumY = 0.0f;
        for (int i = 0; i < 12; i++) {
            cal.add(Calendar.MONTH, (i == 0) ? 0 : -1);
            String date = dateFormat.format(cal.getTime());
            String[] value = salesData.getSalesDataForMonth(date);
            String[] newValue = {value[0] == null ? "0" : value[0], value[1] == null ? "0" : value[1], date};
            totalSumY += Float.parseFloat(value[0] == null ? "0" : value[0]);
            values.add(newValue);
        }
        renderSalesGraphForMonths(values, totalSumY);
    }

    public void initChart() {
        FrameLayout dayButton = (FrameLayout) getActivity().findViewById(R.id.day_button);
        FrameLayout monthButton = (FrameLayout) getActivity().findViewById(R.id.month_button);
        dayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ((TextView) getActivity().findViewById(R.id.button_day_text)).setTextColor(
                            getResources().getColor(R.color.dark_darkblue)
                    );
                    ((TextView) getActivity().findViewById(R.id.button_day_text)).
                            setBackground(getResources().getDrawable(R.drawable.button_border_active));

                    ((TextView) getActivity().findViewById(R.id.button_month_text)).setTextColor(
                            getResources().getColor(R.color.default_text_color)
                    );
                    ((TextView) getActivity().findViewById(R.id.button_month_text)).
                            setBackground(getResources().getDrawable(R.drawable.button_border));

                    generateTablesRows(Calendar.getInstance(), 60, -1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        monthButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    ((TextView) getActivity().findViewById(R.id.button_month_text)).setTextColor(
                            getResources().getColor(R.color.dark_darkblue)
                    );
                    ((TextView) getActivity().findViewById(R.id.button_month_text)).
                            setBackground(getResources().getDrawable(R.drawable.button_border_active));

                    ((TextView) getActivity().findViewById(R.id.button_day_text)).setTextColor(
                            getResources().getColor(R.color.default_text_color)
                    );
                    ((TextView) getActivity().findViewById(R.id.button_day_text)).
                            setBackground(getResources().getDrawable(R.drawable.button_border));
                    getMonths();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public void generateTablesRows(Calendar cal, int limit, int increment) throws Exception {
        salesListContainer.removeAllViews();
        float totalSumY = 0.0f;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        List<String[]> barGraphValues = new ArrayList<>();
        for (int i = 0; i < limit; i++) {
            cal.add(Calendar.DATE, (i == 0) ? 0 : increment);
            String date = dateFormat.format(cal.getTime());
            String[] value = addTOSalesTable(date, (i == 0) ? true : false);
            totalSumY += Float.parseFloat((value[0] == null) ? "1" : value[0]);
            barGraphValues.add(value);
        }
        renderSalesGraph(barGraphValues, totalSumY);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showPopUp();
        layoutInflater = LayoutInflater.from(getContext());
        salesListContainer = (LinearLayout) (getActivity().findViewById(R.id.sales_list_container));
        salesListContainer.removeAllViews();
        salesBarConatainer = (LinearLayout) getActivity().findViewById(R.id.bars_container);
        salesBarConatainer.removeAllViews();
        new SalesTabLoader().execute();

    }

    public class MonthBarClickListener implements View.OnClickListener {
        Calendar cal;

        public MonthBarClickListener(Calendar cal) {
            this.cal = cal;
        }

        @Override
        public void onClick(View v) {
            try {
                //new SalesAsyncTask(cal).execute(cal.getActualMaximum(Calendar.DAY_OF_MONTH),1);
                generateTablesRows(cal, cal.getActualMaximum(Calendar.DAY_OF_MONTH), 1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class SalesTabLoader extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            salesData = new SalesData(getContext());
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            try {
                initChart();
                generateTablesRows(Calendar.getInstance(), 60, -1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                synchronizeScrollers();
                progressDialog.dismiss();
            }
        }
    }

}

4 个答案:

答案 0 :(得分:2)

试试这样:

 progressDialog = new ProgressDialog(getActivity()); 

如果您希望自定义对话框并将自己创建的布局放入其中。

/**
 * Created by vivek on 18/10/16.
 */
public class CustomDialog {
    private static Dialog dialog;
    private static Context context;

    public CustomDialog(Context context) {
        this.context = context;
    }

    /**
     * Comman progress dialog ... initiates with this
     *
     * @param message
     * @param title
     */
    public static void showProgressDialog(Context context, String title, String message) {
        if (dialog == null)
        {
            dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custom_loader);
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }
    }

    public static boolean isProgressDialogRunning() {
        if (dialog != null && dialog.isShowing()) {
            return true;
        } else return false;
    }

    /**
     * Dismiss comman progress dialog
     */
    public static void dismissProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }
} // End of main class over here ...

答案 1 :(得分:1)

替换它: -

SELECT regexp_split_to_table(feature, E' \\| ') AS k, count(*)
FROM tab1
GROUP BY k

progressDialog = new ProgressDialog(getContext(), ProgressDialog.THEME_HOLO_LIGHT);

答案 2 :(得分:0)

可能是。你在屏幕上显示了两个progressdialog以及progressbar这两个{但是当你在那个时候解雇你的progressbar时,在你解雇第一个之前,前一个被其他人覆盖..所以第一个人总是在屏幕上。这就是问题所在。

答案 3 :(得分:0)

我尝试了一切。但没有一个答案解决了这个问题。但是,我通过覆盖片段中的onResume()方法来解决它。

全部谢谢。