0

I have a AutoCompleteTextView on my map page. It works perfectly fine and the predicted options are just how i want them to be. But after the user clicks on one of the predictions (or enters an address on his own), I want to change the address in the AutoComplete view to a string and get a latitude and longitude based on that. I want this to be done when he presses the done button on the keyboardThe following is my code for this

from_loc = (AutoCompleteTextView) findViewById(R.id.from_address);
from_loc.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));

from_loc.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_ENTER ){

                JSONObject startObj = new JSONObject();
                startObj = getLocationInfo(from_loc.getText().toString());

                try {
                    double start_lat = ((JSONArray)startObj.get("results")).getJSONObject(0)
                            .getJSONObject("geometry").getJSONObject("location")
                            .getDouble("lng");

                    double start_long = ((JSONArray)startObj.get("results")).getJSONObject(0)
                            .getJSONObject("geometry").getJSONObject("location")
                            .getDouble("lat");

                    MarkerOptions marker = new MarkerOptions().position(new LatLng(start_lat, start_long)).title("Place1");

                    map.addMarker(marker);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return true;
            }
            return false;
        }

    });

My getLocationInfo method is as follows:

public static JSONObject getLocationInfo(String address) {

    StringBuilder stringBuilder = new StringBuilder();

    try {
        address = address.replaceAll(" ","%20");    

        HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        stringBuilder = new StringBuilder();


        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}

But this code is not working. Even when I tried to display the text from the AutoComplete text view in another normal text View, it didn't happen. In fact when I debugged the code, I realized that it is never going inside the from_loc.setOnKeyListener part but don't know what needs to be done. I even tried implementing the Listener for the the Adapter but nothing happened.

Ankush
  • 6,767
  • 8
  • 30
  • 42

1 Answers1

0

Are you using the soft keyboard (IME)? OnKeyListener only gets invoked with events from the hardware keyboard. You can use:

Or

[Source: http://developer.android.com/reference/android/view/View.OnKeyListener.html]

Community
  • 1
  • 1
Andrea Motto
  • 1,540
  • 21
  • 38