title: Autocomplete textview google places api
body: <p>I followed <a href="http://wptrafficanalyzer.in/blog/android-autocompletetextview-with-google-places-autocomplete-api/" rel="nofollow">this tutorial</a> for doing the autocomplete textview using google places api. I want an autocomplete textview that automatically fills itself by the place names as we start typing. The above referred tutorial is exactly what I require but it is not working for me. The autofill action is not taking place.Can someone suggest a way out?</p>
  
  <p>Here is the main activity</p>
  
  <pre><code> public class MainActivity extends Activity {
  
  AutoCompleteTextView atvPlaces;
  PlacesTask placesTask;
  ParserTask parserTask;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
      atvPlaces.setThreshold(1);
  
      atvPlaces.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              placesTask = new PlacesTask();
              placesTask.execute(s.toString());
          }
  
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count,
          int after) {
              // TODO Auto-generated method stub
          }
  
          @Override
          public void afterTextChanged(Editable s) {
              // TODO Auto-generated method stub
          }
      });
  }
  
  /** A method to download json data from url */
  private String downloadUrl(String strUrl) throws IOException{
      String data = "";
      InputStream iStream = null;
      HttpURLConnection urlConnection = null;
      try{
          URL url = new URL(strUrl);
  
          // Creating an http connection to communicate with url
          urlConnection = (HttpURLConnection) url.openConnection();
  
          // Connecting to url
          urlConnection.connect();
  
          // Reading data from url
          iStream = urlConnection.getInputStream();
  
          BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
  
          StringBuilder sb = new StringBuilder();
  
          String line = "";
          while( ( line = br.readLine()) != null){
              sb.append(line);
          }
  
          data = sb.toString();
  
          br.close();
  
      }catch(Exception e){
          Log.d("Exception while downloading url", e.toString());
      }finally{
          iStream.close();
          urlConnection.disconnect();
      }
      return data;
  }
  
  // Fetches all places from GooglePlaces AutoComplete Web Service
  private class PlacesTask extends AsyncTask&lt;String, Void, String&gt;{
  
      @Override
      protected String doInBackground(String... place) {
          // For storing data from web service
          String data = "";
  
          // Obtain browser key from https://code.google.com/apis/console
          String key = "key=AIzaSyBB5KBkuA4-qm8QxaXX8FhHqhHsESMdAzI";
  
          String input="";
  
          try {
              input = "input=" + URLEncoder.encode(place[0], "utf-8");
          } catch (UnsupportedEncodingException e1) {
              e1.printStackTrace();
          }
  
          // place type to be searched
          String types = "types=geocode";
  
          // Sensor enabled
          String sensor = "sensor=false";
  
          // Building the parameters to the web service
          String parameters = input+"&amp;"+types+"&amp;"+sensor+"&amp;"+key;
  
          // Output format
          String output = "json";
  
          // Building the url to the web service
          String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
  
          try{
              // Fetching the data from we service
              data = downloadUrl(url);
          }catch(Exception e){
              Log.d("Background Task",e.toString());
          }
          return data;
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
  
          // Creating ParserTask
          parserTask = new ParserTask();
  
          // Starting Parsing the JSON string returned by Web Service
          parserTask.execute(result);
      }
  }
  /** A class to parse the Google Places in JSON format */
  private class ParserTask extends AsyncTask&lt;String, Integer, List&lt;HashMap&lt;String,String&gt;&gt;&gt;{
  
      JSONObject jObject;
  
      @Override
      protected List&lt;HashMap&lt;String, String&gt;&gt; doInBackground(String... jsonData) {
  
          List&lt;HashMap&lt;String, String&gt;&gt; places = null;
  
          PlaceJSONParser placeJsonParser = new PlaceJSONParser();
  
          try{
              jObject = new JSONObject(jsonData[0]);
  
              // Getting the parsed data as a List construct
              places = placeJsonParser.parse(jObject);
  
          }catch(Exception e){
              Log.d("Exception",e.toString());
          }
          return places;
      }
  
      @Override
      protected void onPostExecute(List&lt;HashMap&lt;String, String&gt;&gt; result) {
  
          String[] from = new String[] { "description"};
          int[] to = new int[] { android.R.id.text1 };
  
          // Creating a SimpleAdapter for the AutoCompleteTextView
          SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);
  
          // Setting the adapter
          atvPlaces.setAdapter(adapter);
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  }
  </code></pre>
  
  <p>This is the PlaceJSONParser.java</p>
  
  <pre><code> public class PlaceJSONParser {
  
  /** Receives a JSONObject and returns a list */
  public List&lt;HashMap&lt;String,String&gt;&gt; parse(JSONObject jObject){
  
      JSONArray jPlaces = null;
      try {
          /** Retrieves all the elements in the 'places' array */
          jPlaces = jObject.getJSONArray("predictions");
      } catch (JSONException e) {
          e.printStackTrace();
      }
      /** Invoking getPlaces with the array of json object
       * where each json object represent a place
      */
      return getPlaces(jPlaces);
  }
  
  private List&lt;HashMap&lt;String, String&gt;&gt; getPlaces(JSONArray jPlaces){
      int placesCount = jPlaces.length();
      List&lt;HashMap&lt;String, String&gt;&gt; placesList = new ArrayList&lt;HashMap&lt;String,String&gt;&gt;();
      HashMap&lt;String, String&gt; place = null;
  
      /** Taking each place, parses and adds to list object */
      for(int i=0; i&lt;placesCount;i++){
          try {
              /** Call getPlace with place JSON object to parse the place */
              place = getPlace((JSONObject)jPlaces.get(i));
              placesList.add(place);
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  
      return placesList;
  }
  
  /** Parsing the Place JSON object */
  private HashMap&lt;String, String&gt; getPlace(JSONObject jPlace){
  
      HashMap&lt;String, String&gt; place = new HashMap&lt;String, String&gt;();
  
      String id="";
      String reference="";
      String description="";
  
      try {
  
          description = jPlace.getString("description");
          id = jPlace.getString("id");
          reference = jPlace.getString("reference");
  
          place.put("description", description);
          place.put("_id",id);
          place.put("reference",reference);
  
      } catch (JSONException e) {
          e.printStackTrace();
      }
      return place;
  }
  }
  </code></pre>
  
  <p>This is the CustomAutoCompleteTextView.java</p>
  
  <pre><code> public class CustomAutoCompleteTextView extends AutoCompleteTextView {
  
  public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
  }
  
  /** Returns the place description corresponding to the selected item */
  @Override
  protected CharSequence convertSelectionToString(Object selectedItem) {
      /** Each item in the autocompetetextview suggestion list is a hashmap object */
      HashMap&lt;String, String&gt; hm = (HashMap&lt;String, String&gt;) selectedItem;
      return hm.get("description");
  }
  }
  </code></pre>
  
  <p>This is the manifest file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.map5"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="18" /&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name="com.example.map5.MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>