在点击时从服务器获取数据时如何设置进度条?

时间:2014-08-22 07:49:01

标签: java android

我想在未打开新活动时添加进度条。 在下一个活动中我也在提取数据,所以我想在下一个活动中添加一个进度条。

这是我的代码。

login=(Button)dialog.findViewById(R.id.buttonLogin);
login.setOnClickListener(new OnClickListener() {

                    @SuppressLint("DefaultLocale")
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        if(LoginUsername.getText()==null||LoginUsername.getText().toString().equals(""))
                        {
                            LoginUsername.setHint("Enter Username");
                            LoginUsername.setHintTextColor(Color.RED);
                        }
                        else if(LoginPassword.getText()==null||LoginPassword.getText().toString().equals("")||LoginPassword.getText().toString().length()<6)
                        {
                                LoginPassword.setText("");
                                LoginPassword.setHint("Enter Password");
                                LoginPassword.setHintTextColor(Color.RED);
                        }
                        else
                        {
                            String username=LoginUsername.getText().toString();
                            String password=LoginPassword.getText().toString();
                            username1=username.toLowerCase();

                            // fetch the Password form database for respective user name
                            //String loginentries=database.getSingleEntry(username1);
                            //type=database.getType(username1);

                            try{
                                HttpClient client=new DefaultHttpClient();
                                HttpPost post=new HttpPost("http://www.universal-cinemas.com/android/login.php");
                                JSONObject jobj=new JSONObject();
                                jobj.put("username",username1);
                                jobj.put("password", password);
                                post.setEntity(new StringEntity(jobj.toString()));
                                Log.i("Info", "Sending request");
                                HttpResponse res=client.execute(post);
                                Log.i("Info", "Executed");
                                InputStream inp=res.getEntity().getContent();
                                BufferedReader bf = new BufferedReader(new InputStreamReader(inp));
                                StringBuilder sb= new StringBuilder();
                                sb.append(bf.readLine()+"\n");
                                String tmp="0";
                                while((tmp=bf.readLine())!=null)
                                {
                                    sb.append(tmp+"\n");
                                }

                                String result= sb.toString();
                                JSONArray jarray=new JSONArray(result);

                                for(int i=0;i<jarray.length();i++)
                                {
                                    a=1;
                                    JSONObject job=jarray.getJSONObject(i);
                                    type=job.getString("type");
                                    currency=job.getString("currency");
                                }
                            }

                            catch(Exception e)
                            {   
                                e.printStackTrace();
                            }

                            if(a==1)
                            {
                                i=new Intent(getApplicationContext(),User_MainOptions_List.class);
                                startActivity(i);
                                finish();
                                Toast.makeText(getApplicationContext(), "Welcome "+username, Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                                Toast.makeText(getApplicationContext(), "Username and Password is not correct", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }
                });

                dialog.show();

                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); // retrieves the windows attributes
                lp.dimAmount=0.7f;// sets the dimming amount to zero
                dialog.getWindow().setAttributes(lp); // sets the updated windows attributes
                dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); // adds the flag to blur bg

            }
        });

2 个答案:

答案 0 :(得分:0)

 class MyLodingAsycTask extends AsyncTask<Void, Void, Void>{
            private ProgressDialog progressDialog;




            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                runOnUiThread(new Runnable() {
                    public void run() {
                        progressDialog = new ProgressDialog(CameraActivity.this);
                        progressDialog.setMessage("Loding...");
                        progressDialog.setCancelable(false);
                        progressDialog.show();
                    }
                });
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                runOnUiThread(new Runnable() {
                    public void run() {
                        if(progressDialog.isShowing())
                            progressDialog.dismiss();

                    }
                });
            }

            @Override
            protected Void doInBackground(Void... params) {
                //call HTTP service
                return null;
            }

        }

答案 1 :(得分:0)

试试这个

private class MyAsync extends AsyncTask {
  ProgressDialog PD;

  @Override
  protected void onPreExecute() {

   super.onPreExecute();
   PD = new ProgressDialog(MainActivity.this);
   PD.setTitle("Please Wait..");
   PD.setMessage("Loading...");
   PD.setCancelable(false);
   PD.show();
  }

  @Override
  protected Void doInBackground(Void... params) {
   //do what u want
   return result;
  }

  @Override
  protected void onPostExecute(Void result) {  
   super.onPostExecute(result);
   PD.dismiss();
  }
 }
}