Android Open Source - meeting-app Places Autocomplete Adapter






From Project

Back to project page meeting-app.

License

The source code is released under:

Apache License

If you think the Android project meeting-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Google Inc./* w ww  .ja  va  2  s  .c om*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.meetingapp;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import com.example.wrappers.PlaceAutocompletePrediction;
import com.example.wrappers.PlacesAutocomplete;
import com.example.wrappers.PlacesAutocomplete.Request;
import com.example.wrappers.PlacesAutocompleteResponse;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

/**
 * TODO: Insert description here. (generated by ung)
 */
public class PlacesAutocompleteAdapter extends ArrayAdapter<String> implements Filterable {
  private static final String TAG = 
      PlacesAutocompleteAdapter.class.getSimpleName();
  
  private static final PlacesAutocomplete mAutocompleteService
    = new PlacesAutocomplete("AIzaSyCtDiLiH1Qlb1PWogd8bgn8Ww8NK2_CQLk");
  
  private ArrayList<String> resultList;

  public PlacesAutocompleteAdapter(Context context, int textViewResourceId) {
      super(context, textViewResourceId);
  }

  @Override
  public int getCount() {
      return resultList.size();
  }

  @Override
  public String getItem(int index) {
      return resultList.get(index);
  }

  @Override
  public Filter getFilter() {
    Filter filter = new Filter() {
      @Override
      protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();
        if (constraint != null) {
          // Retrieve the autocomplete results.
          GetSuggestionsTask task = new GetSuggestionsTask();
          task.execute(constraint.toString().replace(" ", "+"));
          try {
            resultList = task.get();
          } catch (ExecutionException e) {
            throw new RuntimeException(e);
          } catch (InterruptedException e) {
            throw new RuntimeException(e);
          }

          // Assign the data to the FilterResults
          filterResults.values = resultList;
          filterResults.count = resultList.size();
        }
        return filterResults;
      }

      @Override
      protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results != null && results.count > 0) {
          notifyDataSetChanged();
        } else {
          notifyDataSetInvalidated();
        }
      }
    };
    return filter;
  }
  
  public class GetSuggestionsTask extends AsyncTask<String, Void, ArrayList<String>> {
    @Override
    protected ArrayList<String> doInBackground(String... params) {
      String input = params[0];
      Request request = PlacesAutocomplete.request(true)
            .input(input)
            .build();
      PlacesAutocompleteResponse response;
      try {
        response = mAutocompleteService.lookup(request);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      
      Log.d(TAG, "response: " + response.status);
      Log.d(TAG, "predictions: " + response.predictions.size());
      if (response.predictions != null) {
        ArrayList<String> predictions = new ArrayList<String>();
        for (PlaceAutocompletePrediction prediction : response.predictions) {
          predictions.add(prediction.description);
        }
        return predictions;
      }
      return new ArrayList<String>();
    }
  }
}




Java Source Code List

com.example.geocodeservice.GeocodeResponse.java
com.example.geocodeservice.GeocodeResult.java
com.example.geocodeservice.GeocodeServiceTest.java
com.example.geocodeservice.GeocodeService.java
com.example.meetingapp.LocationParcel.java
com.example.meetingapp.MainActivity.java
com.example.meetingapp.MapResultsFragment.java
com.example.meetingapp.ModifyStateCallback.java
com.example.meetingapp.OptionsFragment.java
com.example.meetingapp.PickCategoryFragment.java
com.example.meetingapp.PickLocationFragment.java
com.example.meetingapp.PlacesAutocompleteAdapter.java
com.example.meetingapp.ResultAdapter.java
com.example.meetingapp.ShowDetailsFragment.java
com.example.meetingapp.ShowResultsFragment.java
com.example.meetingapp.State.java
com.example.placedetails.DetailsResponse.java
com.example.placedetails.DetailsResult.java
com.example.placedetails.DetailsService.java
com.example.placephotos.PhotoService.java
com.example.wrappers.DistanceMatrixResponse.java
com.example.wrappers.DistanceMatrixTest.java
com.example.wrappers.DistanceMatrix.java
com.example.wrappers.LatLng.java
com.example.wrappers.PlaceAutocompletePrediction.java
com.example.wrappers.PlaceCriteria.java
com.example.wrappers.PlaceQuery.java
com.example.wrappers.PlaceResult.java
com.example.wrappers.PlaceSearch.java
com.example.wrappers.PlaceWrapperTest.java
com.example.wrappers.PlaceWrapper.java
com.example.wrappers.PlacesAutocompleteResponse.java
com.example.wrappers.PlacesAutocomplete.java
com.example.wrappers.PlacesResponse.java
com.example.wrappers.PlacesServiceTest.java
com.example.wrappers.PlacesService.java
com.example.wrappers.RankBy.java
com.example.wrappers.StaticMapTest.java
com.example.wrappers.StaticMap.java