显示警报对话框之前不显示进度对话框

时间:2017-05-29 07:00:49

标签: java android

在我的片段中,Listview Onclicklistener被定向到ListTrainSearch。它工作正常,显示警报对话框,但从不显示进度对话框。尝试AsyncTask也是,不成功。试过getActivity()。runOnUiThread,结果相同。任何回复都会有很大的帮助..

public void ListTrainSearch(String spinval) {

mProgressDialog = new ProgressDialog(getContext());

                  mProgressDialog.setMessage("Please Wait!");
                  mProgressDialog.setIndeterminate(true);
                  mProgressDialog.setMax(100);
                  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                  mProgressDialog.setCancelable(false);
                                       mProgressDialog.show();

    spinval = spinval.substring(0, Math.min(spinval.length(), 5));

    if (spinval.matches("\\d*")) {
       HashMap<String, String> temp = new HashMap<String, String>();
        fast_list = new ArrayList<HashMap<String, String>>();
        temp.put(FIRST_COLUMN, "STATION");
        temp.put(SECOND_COLUMN, "TIME");
        fast_list.add(temp);
        Boolean flag = false;

        try {
                           File file = new File(getActivity().getExternalFilesDir(null), "pttmmc.xls");
            InputStream myInput = new FileInputStream(file);
            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
            for (int i = 0; i < myWorkBook.getNumberOfSheets(); i++) {
                String str = "";
                int col_num = 0;
                int k = 0;
                HSSFSheet mySheet = myWorkBook.getSheetAt(i);
               Iterator<Row> rowIterator = mySheet.iterator();
                int j = 0;
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
                    if (row.getRowNum() == 1) {
                        k = 0;
        while (cellIterator.hasNext()) 
        {Cell cell = cellIterator.next();
                           switch (cell.getCellType()) {
                                case Cell.CELL_TYPE_NUMERIC:
                                    str = cell.toString();
                                    break;
                                case Cell.CELL_TYPE_STRING:
                                    str = cell.toString();
                                    break;
                            }
                            if (!str.isEmpty()) {

                                if (str.contains(spinval)) {
                                    col_num = k;
                                    flag = Boolean.TRUE;

                            }
                            k = k + 1;
                        }
                    } else if (row.getRowNum() > 1)
                        if (col_num > 0) {
                            String str1 = "";
                            HashMap<String, String> temp1 = new HashMap<String, String>();
                            switch (row.getCell(col_num).getCellType()) {
                                case Cell.CELL_TYPE_NUMERIC:
                                    Double str2 = row.getCell(col_num).getNumericCellValue();
                                    str1 = str2.toString();
                                    if (!str1.isEmpty()) {
                                        if (DateUtil.isCellDateFormatted(row.getCell(col_num))) {
                                            try {
                                                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
                                                str1 = sdf.format(row.getCell(col_num).getDateCellValue());
                                            } catch (Exception e) {
                                                // Toast.makeText(getContext(), "Crashed " + str1, Toast.LENGTH_SHORT).show();
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                    // Toast.makeText(getContext(), "Numeric " + str1, Toast.LENGTH_SHORT).show();
                                    break;
                                case Cell.CELL_TYPE_STRING:
                                    str1 = row.getCell(col_num).toString();
                                    //   Toast.makeText(getContext(), "String " + str1, Toast.LENGTH_SHORT).show();
                                    break;
                            }
                           if (!str1.isEmpty()) {
                               temp1.put(FIRST_COLUMN, row.getCell(1).toString());
                                temp1.put(SECOND_COLUMN, str1);
                                fast_list.add(temp1);
                                 Toast.makeText(getContext(), row.getCell(1).toString()+" : " + str, Toast.LENGTH_SHORT).show();
                            }
                        }
                    j = j + 1;
                    }

            }
        } catch (Exception e) {         //****   If xls file not found
             mProgressDialog.dismiss();
            new AlertDialog.Builder(getContext())
                    .setTitle("MOTORMAN GUIDE")
                    .setMessage("Please perform \"Check Update -> PTT Update\"")
                    // .setNegativeButton(android.R.string.no, null)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                            //  MainActivity.super.onBackPressed();
                        }
                    })
                    .create().show();
            e.printStackTrace();
        }
        if (flag) {   //*  If string match found, display alert dialog
    mProgressDialog.dismiss();
           ListViewAdapters adapter = new ListViewAdapters(getActivity(), fast_list);
           new AlertDialog.Builder(getContext())
                    .setTitle("Train no: " + spinval)
                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                        }
                    })
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                            //  MainActivity.super.onBackPressed();
                        }
                    })
                    .create().show();
        } else { //*  If string match not found display toast
             mProgressDialog.dismiss();
            Toast.makeText(getContext(), "Train Number " + spinval + " not Found ", Toast.LENGTH_SHORT).show();
        }
    } else {//*  If string didn't match "\\d*"
         mProgressDialog.dismiss();
        Toast.makeText(getContext(), "Invalid Train Number ", Toast.LENGTH_SHORT).show();
    }

}**

1 个答案:

答案 0 :(得分:1)

试试这个:

Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                  mProgressDialog.setMessage("Please Wait!");
                  mProgressDialog.setIndeterminate(true);
                  mProgressDialog.setMax(100);
                  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                  mProgressDialog.setCancelable(false);
                  mProgressDialog.show();
            }
        };

也许您需要使用其他 处理程序 解雇 对话