1

我正在构建一个应用程序,该应用程序将使用GeoCoder从用户输入中获取街道地址。特此我制作的一段代码:

Geocoder gc = new Geocoder(getBaseContext(), Locale.getDefault());

List<Address> addresses = null;
StringBuilder sb = new StringBuilder();
String destination = edittext_destination.getText().toString();

try {
    addresses = gc.getFromLocationName(destination, 10);
} catch (Exception e){
    Toast.makeText(getBaseContext(), "Address not found", Toast.LENGTH_SHORT).show();
}

上面的代码正在运行,但返回结果需要一些时间。在等待结果时,我想显示进度微调器。我知道它应该使用Thread但我不知道如何开始。我希望任何人都可以提供帮助。

谢谢

4

2 回答 2

1

你可以这样做AsyncTask

    final String destination = edittext_destination.getText().toString();
    new AsyncTask<String, Void, List<Address>>() {
        private Dialog loadingDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loadingDialog = ProgressDialog.show(TestActivity.this, "Please wait", "Loading addresses...");
        }

        @Override
        protected List<Address> doInBackground(String... params) {
            String destination = params[0];
            try {
                Geocoder gc = new Geocoder(getBaseContext(),
                        Locale.getDefault());
                return gc.getFromLocationName(destination, 10);
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(List<Address> addresses) {
            loadingDialog.dismiss();

            if (addresses == null) {
                Toast.makeText(getBaseContext(), "Geocoding error",
                        Toast.LENGTH_SHORT).show();
            } else if (addresses.size() == 0) {
                Toast.makeText(getBaseContext(), "Address not found",
                        Toast.LENGTH_SHORT).show();
            } else {
                // Do UI stuff with your addresses
                Toast.makeText(getBaseContext(), "Addresses found: " + addresses.size(), Toast.LENGTH_SHORT).show();
            }
        }
    }.execute(destination);
于 2012-11-20T09:57:02.580 回答
0

Android Query让所有这些异步的东西变得超级简单。您可以添加微调器和进度条,而不是像这样的异步调用:

设置:

ProgressDialog dialog = new ProgressDialog(this);

dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle("Sending...");

String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";                

你异步调用:

aq.progress(dialog).ajax(url, JSONObject.class, this, "jsonCb");

看看这个!优秀的响应时间和来自 Peter Liu 的帮助。该项目使开发人员受益!

于 2013-04-04T21:18:21.003 回答