ProgressDialog显示加载android后

时间:2014-07-12 21:58:26

标签: android progressdialog

我已经在她的办公室preexecute中创建了一个扩展AsyncTask的类,并且显示了progressdialog,然后进入doinbackground,我调用url并转换为JSON字符串,并在postexecute函数之后放入progressdialog dismiss,并且在所有之后可以加载和删除,但中间有一个圆圈,表示加载。见图片...... IMAGE SCREEN看屏幕!

 private class DownloadJSON extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // kriranje progressdialog-a
        mProgressDialog = new ProgressDialog(Info.this.getActivity());

        mProgressDialog.setMessage("Učitavanje informacija...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://77.105.36.203/objects.txt");
        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("objects");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("ime", jsonobject.getString("ime"));
                map.put("adresa", jsonobject.getString("adresa"));
                map.put("email", jsonobject.getString("email"));
                map.put("slika", jsonobject.getString("slika"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Greska:", e.getMessage());
            e.printStackTrace();
           //finish();
        } catch (Exception a){
            Log.e("errr", "ss");
            a.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        listview = (ListView) getView().findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(Info.this.getActivity(), arraylist);
        listview.setAdapter(adapter);
        mProgressDialog.dismiss();
    }

} 

1 个答案:

答案 0 :(得分:0)

我没有看到任何直接明显的原因导致这个问题。

有些值得解决的问题是在私有AsyncTask类之外大量使用字段变量。我认为让AsyncTask尽可能自包含是一种更好的做法。

尽管如此,这是一个与我的代码有点类似的情况,使用它没有任何问题。注意我如何将找到的文件传递给onPostExcecute,认为你对你的arraylist做同样的事情会更干净,尽管我不认为这与你的问题有关。

private class DownloadUpdate extends AsyncTask<Void, Void, File> {

    private ProgressDialog working_dialog;

    @Override protected void onPreExecute() {
        working_dialog = ProgressDialog.show(context, "",
                getString(R.string.working_please_wait), true);
    }

    @Override protected File doInBackground(Void... param) {

        String filename = "turios.apk";

        HttpURLConnection c = null;
        try {
            URL url = new URL(Constants.UPDATE_DOWNLOAD_URL);
            c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
        } catch (IOException e1) {
            // TODO handle
        }

        File myFilesDir = new File(Environment
                .getExternalStorageDirectory().getAbsolutePath()
                + "/Download");

        File file = new File(myFilesDir, filename);

        if (file.exists()) {
            file.delete();
        }

        if ((myFilesDir.mkdirs() || myFilesDir.isDirectory())) {
            try {
                InputStream is = c.getInputStream();
                FileOutputStream fos = new FileOutputStream(myFilesDir
                        + "/" + filename);

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

            } catch (Exception e) {
                // TODO handle
            }

        }

        return file;
    }

    @Override protected void onPostExecute(File file) {

        String toastMsg = "";
        if (file == null) {
            toastMsg = getString(R.string.could_not_create_or_access_downloadfolder);
            ;
        } else if (file.exists()) {
            installer.launchInstaller(file);
            toastMsg = getString(R.string.file_successfully_downloaded);
        } else {
            toastMsg = getString(R.string.could_not_locate_file);
        }

        if (!toastMsg.isEmpty()) {
            makeToast(toastMsg);
        }

        removeWorkingDialog();
    }

    private void removeWorkingDialog() {
        if (working_dialog != null) {
            working_dialog.dismiss();
            working_dialog = null;
        }
    }
}