-1

我正在尝试生成一个进度条来告诉我到目前为止我下载的文件的百分比。进度条正在显示,但它永远不会更新,它保持静态。

while((line = reader.readLine()) != null) { 
                    values.add(line);             

                }
                dialog = new ProgressDialog(ourContext);
                dialog.setCancelable(true);
                dialog.setMessage("File downloading...");
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.setProgress(0);
                dialog.setMax(values.size());
                dialog.show();

                progressBarStatus = 0;

                fileSize = 0;

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        while(progressBarStatus < values.size()){
                            for(int i = 0; i < values.size();i++){
                                String[] data = values.get(i).split(",");
                                //Log.d("Test drive " ,data[1] );   

                                String[] dateSplit = data[1].split("[-]");
                                String month = dateSplit[1];
                                String day = dateSplit[0];
                                String year = dateSplit[2];

                                Calendar cal = Calendar.getInstance();
                                try {
                                    cal.setTime(new SimpleDateFormat("MMM").parse(month));
                                } catch (ParseException e) {
                                    // TODO Auto-generated catch block
                                    String error = e.toString();
                                    Dialog d = new Dialog(ourContext);
                                    d.setTitle("Error");
                                    TextView tv = new TextView(ourContext);
                                    tv.setText(error);
                                    d.setContentView(tv);
                                    d.show();
                                }

                                int monthInt = cal.get(Calendar.MONTH) + 1;
                                int yearInt = Integer.parseInt(year);
                                int dayInt = Integer.parseInt(day);                             

                                try {
                                    cashPotEntry(dayInt, monthInt, yearInt, data[3], data[2], data[4]);
                                    progressBarStatus++;
                                    Log.d("progress bar" ,progressBarStatus + " "  );                                   
                                    Thread.sleep(1000);
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    String error = e.toString();
                                    Dialog d = new Dialog(ourContext);
                                    d.setTitle("Error");
                                    TextView tv = new TextView(ourContext);
                                    tv.setText(error);
                                    d.setContentView(tv);
                                    d.show();
                                }
                                }
                            progressBarHandler.post(new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    dialog.setProgress(progressBarStatus);
                                }
                            });
                        }
                        if(progressBarStatus >= values.size()){
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                String error = e.toString();
                                Dialog d = new Dialog(ourContext);
                                d.setTitle("Error");
                                TextView tv = new TextView(ourContext);
                                tv.setText(error);
                                d.setContentView(tv);
                                d.show();
                            }
                            dialog.dismiss();
                        }

                    }
                }).start();

        }
4

1 回答 1

1

删除你while的循环。

progressBarHandler.post代码放入for循环中。

现在的情况是,在退出循环之前,您永远不会更新进度条for,然后您也退出while循环。

于 2013-09-04T16:19:27.927 回答