【问题标题】:ProgressDialog show after load android加载android后显示ProgressDialog
【发布时间】:2014-07-12 21:58:25
【问题描述】:

我创建了一个类,在她的办公室 preexecute 中扩展 AsyncTask 并显示 progressdialog,然后进入我调用 url 并转换为 JSON 字符串的 doinbackground,并在 postexecute 函数之后将 progressdialog 关闭,并且毕竟是加载和删除,但中间有一个圆圈,上面写着加载。看图片...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();
    }

} 

【问题讨论】:

  • 所以问题是即使在 onPostExecute() 之后还有一个加载指示器?问题/问题我不清楚。
  • 是的,加载后结束 onPostExecute show progressdialog..
  • 从您发布的图像和代码中,我没有看到 Asynctask 中包含正在加载消息的进度微调器代码声明。您应该检查您的 xml 布局和活动。

标签: android progressdialog


【解决方案1】:

我看不出这个问题有任何直接明显的原因。

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

尽管如此,我的代码中有一个类似的场景,使用它没有问题。请注意我如何将找到的文件传递给 onPostExcecute,尽管我认为这与您的问题无关,但您认为对您的数组列表执行相同操作会更干净。

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;
        }
    }
}

【讨论】:

  • 在 ListActivity 上一切正常,但在 ListFragment 上显示进度对话框。
  • 您可以发布片段布局的 XML 吗?只是为了排除这种可能性。
猜你喜欢
  • 2012-04-06
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 2011-09-23
相关资源
最近更新 更多