如何使for循环中的进度条运行

时间:2016-12-07 14:59:13

标签: android android-sqlite android-volley android-progressbar android-database

我有这种方法使用volley从服务器获取数据并使用ormlite将其存储到sqlite数据库中。我在启动画面上这样做。所以现在我想在这个过程发生时显示一些进度条,进度条已经出现,现在我想在for循环中设置进度条的进度。但我不知道为什么我的进度条保持在0%,直到过程结束。当我查看日志时,进度会保持更新,但可能因为它在循环内部没有出现进度。如何让进度出现在progressdialog中?下面是我的代码

 private void fetchMOffice() {
            mOfficeAdapter = new TableMOfficeAdapter(this);
            List<TableMOffice> listOffice = mOfficeAdapter.getAllDatabyLup();

        progressBar = new ProgressDialog(SplashScreenActivity.this);
        progressBar.setCancelable(true);
        progressBar.setMessage("Downloading File...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        showDialog();

        for (int i=0; i<listOffice.size(); i++) {
            TableMOffice item = listOffice.get(i);
            lupMOffice = item.getLup();
            //  locationIdArray.add(item.getKode_office());
            break;
        }
        Log.e(TAG, "lup fetchMOffice: " +lupMOffice );

        StringRequest strReq = new StringRequest(Request.Method.POST,
                ConnectionManager.MASTER_M_OFFICE, new Response.Listener<String>() {


            @Override
            public void onResponse(String response) {
                Log.e(TAG, "response: " + response);



                try {
                    JSONObject obj = new JSONObject(response);
                    dbAdapter = new TableMOfficeAdapter(SplashScreenActivity.this);
                    //  dbAdapter.deleteAll();
                    //   SQLiteStatement insert = mDb.compileStatement(sql);

                    // check for error flag
                    if (obj.getBoolean("error") == false) {
                        jsonArray = obj.getJSONArray("m_office");



                        for ( i = 0; i < jsonArray.length(); i++) {

                                JSONObject versionObj = null;

                                versionObj = (JSONObject) jsonArray.get(i);
                                int id = versionObj.getInt("id");
                                String office = versionObj.getString("office");
                                String kode_office = versionObj.getString("kode_office");
                                String channel = versionObj.getString("channel");
                                String jenis = versionObj.getString("jenis");
                                String alamat = versionObj.getString("alamat");
                                String kota = versionObj.getString("kota");
                                String propinsi = versionObj.getString("propinsi");
                                String pic = versionObj.getString("pic");
                                String pic_phone = versionObj.getString("pic_phone");
                                String map_lat = versionObj.getString("map_lat");
                                String map_lng = versionObj.getString("map_lng");
                                String upd = versionObj.getString("upd");
                                String lup = versionObj.getString("lup");
                                String ket = versionObj.getString("ket");
                                String region = versionObj.getString("region");
                                String rek = versionObj.getString("rek");
                                String bank = versionObj.getString("bank");
                                String rental = versionObj.getString("rental");
                                Log.e("isitabeloffice",   Integer.toString(id));
                                ctx = getApplicationContext();
                                dbAdapter.delete(SplashScreenActivity.this,id);
                                //dbAdapter.delete(ctx,id);
                                dbAdapter.insertData(new TableMOffice(), id, office, kode_office, channel,
                                        jenis, alamat, kota, propinsi, pic, pic_phone, map_lat, map_lng,
                                        upd, lup, ket, region, rek, bank, rental);

                                progress = (i+1)*100/jsonArray.length();
                                progressBar.setProgress(progress);
                                }

                    } else {

                    }

                } catch (Exception e) {
                    Log.e(TAG, "json parsing error: " + e.getMessage());
                    //Toast.makeText(SplashScreenActivity.this, "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                NetworkResponse networkResponse = error.networkResponse;
                Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse);
                hideDialog();
                //Toast.makeText(SplashScreenActivity.this, "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        })
        {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();


                Log.e(TAG, "params: " + params.toString());
                return params;
            }
        };

        MyApplication.getInstance().addToRequestQueue(strReq);
    }

0 个答案:

没有答案