【问题标题】:Waiting progress bar in android等待android中的进度条
【发布时间】:2013-02-26 07:01:15
【问题描述】:

在我的 android 应用程序中,我将一些数据从服务器数据库复制到本地数据库。代码贴在下面。可能需要 15 分钟以上。对于较慢的连接,它可能会被超过。所以我想显示一个等待进度条,在任务完成之前应该显示“请稍候”。我参考了很多东西,但我不明白如何在下面的代码中使用等待进度条。

refresh.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
    mySQLiteAdapter.openToWrite();
    mySQLiteAdapter.deleteAll();
    radapter.openToWrite();
    radapter.deleteAll();
    uadapter.openToWrite();
    uadapter.deleteAll();
            try
                {
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://xxxxxxxxxxxxxxxxxxxxx");
                     HttpResponse response = httpclient.execute(httppost);
                     HttpEntity entity = response.getEntity();
                      is = entity.getContent();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    json = sb.toString();
                } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
                }

                try {
                    jObj = new JSONObject(json);
                    contacts = jObj.getJSONArray("get");
                    for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);
                        if(!c.isNull("rname"))// || c.getString("rcode").equals(null))
                        {
                            rname = c.getString("rname");
                            rcode = c.getString("rcode");
                            radapter.insert(rcode, rname);
                        }
                        else
                        {
                            rname = "";
                            rcode = c.getString("rcode");
                            radapter.insert(rcode, rname);
                        }
                    }
                    Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show();

                } catch (JSONException e) {
                    Toast.makeText(getBaseContext(), "Error"+e.toString(), Toast.LENGTH_LONG).show();
                }
                adapter.deleteAll();
                oadapter.deleteAll();
                e2.setText("Back Up completed");
            }

       });

请有人帮助我。提前致谢

【问题讨论】:

  • 了解水平进度条和线程以相应地处理它。

标签: android progress-bar progressdialog android-progressbar


【解决方案1】:

使用Progress Dialog 显示文本“请稍候...”并在thread 中实现您的代码,如下所示:

      final ProgressDialog progressDialog = ProgressDialog.show(this, "", "Please wait...");
      new Thread() {
          public void run() {
                try{
                      //your code here.....
                   } 
                catch (Exception e) {
                    Log.e("tag", e.getMessage());
                   }
                // dismiss the progress dialog
                       progressDialog.dismiss();
                   }
                }.start();

【讨论】:

  • 将 progressDialog 放在 OnClickListener 之外
  • 我已经尝试过我的活动名称。这个而不是这个。现在我解决了错误并尝试运行我的应用程序
  • 已创建,但突然消失并强制关闭错误
  • 注意progressDialog.dismiss();将关闭对话框。因此,如果您在代码中使用回调,请在 catch 块中将其关闭。我花了几分钟才明白为什么对话框没有打开。
【解决方案2】:

尝试使用 AsyncTask 并使用 onProgressUpdate 方法进行更新

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多