循环内时更新UI上的进度栏

时间:2018-11-09 12:50:35

标签: android loops

我想一劳永逸地做到这一点。 我有一个看起来像这样的字符串请求

StringRequest stringRequest = new StringRequest(Request.Method.POST, MASTER_LINK,
            new Response.Listener<String>() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject obj = new JSONObject(response);
                        JSONArray received_data = obj.getJSONArray("received");
                        if (received_data.length() == 0) {
                            /* TODO : Check what to do in this universe later */
                        } else {

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("email", current_email);
            return params;
        }
    };
    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

上面的代码将执行的操作是使用email参数从php文件创建请求 接下来是什么,它将从SELECT创建一个csv文件,然后返回一个 名为received的json数组,数据为done_cus

之后,如果返回值,它将下载该文件,并且代码是这个

if (done_process.toString().matches("done_cus")) {
    FilePathname_CUS = sdCardDirectory + "/" + for_current_email + "_" + param_dim_customer + ".csv";
    file_link = download_file + for_current_email + "_" + param_dim_customer + ".csv";
    DownloadFilesname(FilePathname_CUS, file_link);
    row_count = row_count + count_row(FilePathname_CUS);
    count_customer = 1;
    Log.e("Downloaded", "Customer List");
}

请查看此count_row,它将计算该文件具有多少行,因此 说我们有1000行,所以如果我有5行,则每1k行就有5k

这是我要执行的下一个操作,我会将其插入我的SQLLite

File file = new File(FilePathname_CUS);
CsvReader csvReader = new CsvReader();
try (CsvParser csvParser = csvReader.parse(file, StandardCharsets.UTF_8)) {
    CsvRow row;
    if (count_row(FilePathname_CUS) != 0) {
        myDb.delete_customer(current_email);
        while ((row = csvParser.nextRow()) != null) {
            myDb.insert_customer(
                    row.getField(0),
                    row.getField(1),
                    row.getField(2),
                    row.getField(3),
                    row.getField(4),
                    row.getField(5),
                    row.getField(6),
                    row.getField(7),
                    row.getField(8),
                    row.getField(9),
                    row.getField(10)
            );
            Log.e("Inserting", "" + row.getField(0));
            index++;
            p = (float) index / (float) row_count;
            p = p * (float) 100;
        }

    } else {

    }
}

请注意这部分

index++;
p = (float) index / (float) row_count;
p = p * (float) 100;

毕竟,这是我的问题,就像现在99%这样的百分比。

如何用该值显示进度栏?

这是我的进度栏

mProgressAnimation.setProgress("Value of the v");

因为我看到的是进度条只会在循环之后才更新。在循环期间如何?

这是整个代码

StringRequest stringRequest = new StringRequest(Request.Method.POST, MASTER_LINK,
            new Response.Listener<String>() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject obj = new JSONObject(response);
                        JSONArray received_data = obj.getJSONArray("received");
                        if (received_data.length() == 0) {
                            /* TODO : Check what to do in this universe later */
                        } else {
                            if (done_process.toString().matches("done_cus")) {
                                FilePathname_CUS = sdCardDirectory + "/" + for_current_email + "_" + param_dim_customer + ".csv";
                                file_link = download_file + for_current_email + "_" + param_dim_customer + ".csv";
                                DownloadFilesname(FilePathname_CUS, file_link);
                                row_count = row_count + count_row(FilePathname_CUS);
                                count_customer = 1;
                                Log.e("Downloaded", "Customer List");
                            }

                            File file = new File(FilePathname_CUS);
                            CsvReader csvReader = new CsvReader();
                            try (CsvParser csvParser = csvReader.parse(file, StandardCharsets.UTF_8)) {
                                CsvRow row;
                                if (count_row(FilePathname_CUS) != 0) {
                                    myDb.delete_customer(current_email);
                                    while ((row = csvParser.nextRow()) != null) {
                                        myDb.insert_customer(
                                                row.getField(0),
                                                row.getField(1),
                                                row.getField(2),
                                                row.getField(3),
                                                row.getField(4),
                                                row.getField(5),
                                                row.getField(6),
                                                row.getField(7),
                                                row.getField(8),
                                                row.getField(9),
                                                row.getField(10)
                                        );
                                        Log.e("Inserting", "" + row.getField(0));
                                        index++;
                                        p = (float) index / (float) row_count;
                                        p = p * (float) 100;
                                        mProgressAnimation.setProgress(p); //this must be update the progress bar
                                    }

                                } else {

                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("email", current_email);
            return params;
        }
    };
    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

0 个答案:

没有答案