1

I am building an application that will street address from user's input using GeoCoder. Herewith piece of code I made:

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

the code above is working but it takes some time to return the result. While waiting for the result I want to display progress spinner. I know that it should use Thread but I don't know how to start. I do hope anyone can help.

Thank you

Bobby
  • 472
  • 5
  • 17

2 Answers2

1

You could do that with an 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);
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • Thank you for the quick respond fiddler. I will try it out. – Bobby Nov 20 '12 at 10:00
  • I was about to post an answer but was caught up with something else. This answer will do, if you add the onPreExecute() method where you show your Spinner and hide it in the onPostExecute() method. – Lazy Ninja Nov 20 '12 at 10:47
  • I have tried to try the code guys. But unfortunately it's not working yet. – Bobby Nov 20 '12 at 11:00
  • I am sorry to tell you guys that the code is not working. I don't know what's wrong. Should I give my whole code here? – Bobby Nov 20 '12 at 11:15
  • 1
    I forgot the `.execute(destination);` in my code...so the task was never executed. Try again with the updated code – sdabet Nov 20 '12 at 11:50
  • Yes it is working now although the application stop responding during the searching. I think it's bug in my code. anyway thank you very much for your help. – Bobby Nov 20 '12 at 12:24
  • By the way a little correction for @fiddler code, inside **onPostExecute** function, `if(addresses == null)` should be `if(address.size() == 0)`. – Bobby Nov 20 '12 at 12:44
  • Then you should use `if(addresses == null || addresses.size() == 0)` because the doInBackground method can return null if operation fails – sdabet Nov 20 '12 at 12:46
0

Android Query makes all this Async stuff super easy. You can add spinners and progress bars with just a little more than an async call like this:

Setup:

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

You async call:

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

Check it out! Excellent response time and help from Peter Liu. This project benefits Devs!

jasonflaherty
  • 1,924
  • 7
  • 40
  • 82