进度对话框的android问题

时间:2011-06-13 09:31:47

标签: android

我有一个应用程序,我从http服务器&获取数据将数据设置为活动。 从服务器获取数据需要时间。所以我想在那里给出一个进度对话框。 我做到了,但问题是progressdialog即将到来,但它不会旋转(只是不变)。 Plz任何人都告诉我它是怎么来的!

以下是我的代码,

private void setupViews()
    {
        imgRestro = (ImageView) findViewById(R.id.imageResturant);
        tvRestroName = (TextView) findViewById(R.id.tvRestroName);
        tvRestroAddr = (TextView) findViewById(R.id.tvRestroAddr);
        tvRestroArea = (TextView) findViewById(R.id.tvRestroArea);
        tvRestroCity = (TextView) findViewById(R.id.tvRestroCity);
        tvRestroPhone = (TextView) findViewById(R.id.tvRestroPhone);
        tvRestroRating = (TextView) findViewById(R.id.tvRestroRating);
        tvRestroCuisines = (TextView) findViewById(R.id.tvRestroCuisines);
        tvRestroAttr = (TextView) findViewById(R.id.tvRestroAttributes);
    }
//to execute url
    private String executeHttpGet(String URL) throws Exception {

        URL url = null;
        StringBuffer strBuf = null;
        BufferedReader read = null;
        HttpURLConnection httprequest = null;
        try {
            url = new URL(URL);
            httprequest = (HttpURLConnection) url.openConnection();
            httprequest.setRequestMethod("GET");
            httprequest.setDoInput(true);
            httprequest.setDoOutput(true);
            httprequest.setConnectTimeout(5000); //time for before connect
            httprequest.setRequestProperty("Content-Type",
                    "text/plain; charset=utf-8");
            //httprequest.setReadTimeout(15000);  //time for after connect

            read = new BufferedReader(new InputStreamReader(
                    httprequest.getInputStream()));

            strBuf = new StringBuffer();

            String line = "";

            while ((line = read.readLine()) != null) {
                strBuf.append(line).append("\n");
            }

        } catch (IOException ex) {

        } finally {
            try {
                httprequest.disconnect();
                read.close();
            } catch (Exception ex) {

            }
        }
        return strBuf.toString();

    }
    //to assign all the info from json to activity textview or imageview
    private void examineJSONFile() {
        try {

            String str = "";
            page = executeHttpGet(url);
            Log.i("hello", str.toString());

            JSONObject topobj = new JSONObject(page);
            JSONObject innerobj = topobj.getJSONObject("restarutant");

            tvRestroName.setText("Restaurant "+innerobj.getString("name"));
            String photoURL = innerobj.getString("photo");
            URI uri = new URI(photoURL);
            Bitmap bmp = loadBitmap(photoURL);
            System.out.println("str ;" + photoURL + "    uri " + uri);

            imgRestro.setImageBitmap(Bitmap.createScaledBitmap(bmp, 300, 300,true));
            tvRestroAddr.setText("Address: " + innerobj.getString("address"));
            tvRestroArea.setText("Area: " + innerobj.getString("area"));
            tvRestroCity.setText("City: " + innerobj.getString("city"));
            JSONArray location = innerobj.getJSONArray("location");
            String lat = location.get(0).toString();
            String lng = location.get(1).toString();
            latitude = Double.valueOf(lat.trim()).doubleValue();
            longitude = Double.valueOf(lng.trim()).doubleValue();
            System.out.println("Lat :" + latitude + " Long :" + longitude);

            JSONArray phone = innerobj.getJSONArray("phone");
            tvRestroPhone.setText("Phone: " + phone.get(0).toString() + " ,"
                    + phone.get(1).toString());

            tvRestroRating.setText("Rating: " + innerobj.getString("rating"));

            JSONArray cuisines = innerobj.getJSONArray("cuisines");
            tvRestroCuisines.setText("Cuisines: " + cuisines.get(0).toString()
                    + " ," + cuisines.get(1).toString());

            JSONArray attributes = innerobj.getJSONArray("attributes");
            tvRestroAttr.setText("Attributes: " + attributes.get(0).toString()
                    + " ," + attributes.get(1).toString() + " ,"
                    + attributes.get(2).toString());

            Log.i("hello", str.toString());

        } catch (Exception je) {
            tvRestroAddr.setText("Error w/file: " + je.getMessage());
        }
    }

    // to get image from url in form of bitmap
    private static Bitmap loadBitmap(String url) {
        URL myFileUrl = null;
        InputStream is = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            Log.i("im connected", "Download");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return BitmapFactory.decodeStream(is);
    }

    //for progressdialog
     private class DownloadTask extends AsyncTask<Void, Void, Void> {
            private ProgressDialog dialog = new ProgressDialog(InfoActivity.this);

            protected void onPreExecute() {
                System.out.println("In onPreExecute "); 
                dialog.setTitle("Loading..");
                dialog.setMessage("Downloading source..");
                dialog.setProgress(100);
                dialog.setMax(5000);
                dialog.show();
            }

            protected Void doInBackground(Void... unused) {
                //examineJSONFile();
                System.out.println("In doInBackground ");   
                return null;
            }


            protected void onPostExecute(Void unused) {
                System.out.println("In onPostExecute ");    
                     dialog.dismiss();
                         examineJSONFile();         
}

Plz看看并给出答案。 谢谢

2 个答案:

答案 0 :(得分:1)

您应该覆盖onProgressUpdate()中的AsyncTask方法并在那里更改对话框的值。您可以通过publishProgress()中的doInBackground()方法将进度值发送到对话框。希望这会有所帮助。

答案 1 :(得分:0)

尝试使用Async任务,如下所示:

try{
    class test extends AsyncTask{


 TextView tv_per;
 int mprogress;

Dialog UpdateDialog = new Dialog(ClassContext);

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    mprogress = 0;

    UpdateDialog.setTitle(getResources().getString(R.string.app_name));
    UpdateDialog.setContentView(R.layout.horizontalprogressdialog);
    TextView dialog_message =  (TextView)UpdateDialog.findViewById(R.id.titleTvLeft);
    tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
    dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data));
    dialog_message.setGravity(Gravity.RIGHT);
    UpdateDialog.setCancelable(false);
    UpdateDialog.show();
    super.onPreExecute();
}



@Override
protected void onProgressUpdate(Object... values) {
    // TODO Auto-generated method stub
    ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar);
    update.setProgress((Integer) values[0]);
    int percent =  (Integer) values[0];
 if(percent>=100)
    {
        percent=100;
    }
    tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
     tv_per.setText(""+percent);
}



@Override
protected Object doInBackground(Object... params) {
    // TODO Auto-generated method stub
    //your code
}

    super.onPostExecute(result);
    UpdateDialog.dismiss();
}

}
new test().execute(null);

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