Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String TAG = "PlaceAutocompleteAd";
- private static final CharacterStyle STYLE_BOLD = new StyleSpan(Typeface.BOLD);
- /**
- * Current results returned by this adapter.
- */
- private ArrayList<AutocompletePrediction> mResultList;
- /**
- * Handles autocomplete requests.
- */
- private GeoDataClient mGeoDataClient;
- /**
- * The bounds used for Places Geo Data autocomplete API requests.
- */
- private LatLngBounds mBounds;
- /**
- * The autocomplete filter used to restrict queries to a specific set of place types.
- */
- private AutocompleteFilter mPlaceFilter;
- /**
- * Initializes with a resource for text rows and autocomplete query bounds.
- *
- * @see android.widget.ArrayAdapter#ArrayAdapter(android.content.Context, int)
- */
- public PlaceAutocompleteAdapter(Context context, GeoDataClient geoDataClient,
- LatLngBounds bounds, AutocompleteFilter filter) {
- super(context, android.R.layout.simple_expandable_list_item_2, android.R.id.text1);
- mGeoDataClient = geoDataClient;
- mBounds = bounds;
- mPlaceFilter = filter;
- }
- /**
- * Sets the bounds for all subsequent queries.
- */
- public void setBounds(LatLngBounds bounds) {
- mBounds = bounds;
- }
- /**
- * Returns the number of results received in the last autocomplete query.
- */
- @Override
- public int getCount() {
- return mResultList.size();
- }
- /**
- * Returns an item from the last autocomplete query.
- */
- @Override
- public AutocompletePrediction getItem(int position) {
- return mResultList.get(position);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View row = super.getView(position, convertView, parent);
- // Sets the primary and secondary text for a row.
- // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
- // styling based on the given CharacterStyle.
- AutocompletePrediction item = getItem(position);
- TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
- TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
- textView1.setText(item.getPrimaryText(STYLE_BOLD));
- textView2.setText(item.getSecondaryText(STYLE_BOLD));
- return row;
- }
- /**
- * Returns the filter for the current set of autocomplete results.
- */
- @Override
- public Filter getFilter() {
- return new Filter() {
- @Override
- protected FilterResults performFiltering(CharSequence constraint) {
- FilterResults results = new FilterResults();
- // We need a separate list to store the results, since
- // this is run asynchronously.
- ArrayList<AutocompletePrediction> filterData = new ArrayList<>();
- // Skip the autocomplete query if no constraints are given.
- if (constraint != null) {
- // Query the autocomplete API for the (constraint) search string.
- filterData = getAutocomplete(constraint);
- }
- results.values = filterData;
- if (filterData != null) {
- results.count = filterData.size();
- } else {
- results.count = 0;
- }
- return results;
- }
- @Override
- protected void publishResults(CharSequence constraint, FilterResults results) {
- if (results != null && results.count > 0) {
- // The API returned at least one result, update the data.
- mResultList = (ArrayList<AutocompletePrediction>) results.values;
- notifyDataSetChanged();
- } else {
- // The API did not return any results, invalidate the data set.
- notifyDataSetInvalidated();
- }
- }
- @Override
- public CharSequence convertResultToString(Object resultValue) {
- // Override this method to display a readable result in the AutocompleteTextView
- // when clicked.
- if (resultValue instanceof AutocompletePrediction) {
- return ((AutocompletePrediction) resultValue).getFullText(null);
- } else {
- return super.convertResultToString(resultValue);
- }
- }
- };
- }
- /**
- * Submits an autocomplete query to the Places Geo Data Autocomplete API.
- * Results are returned as frozen AutocompletePrediction objects, ready to be cached.
- * Returns an empty list if no results were found.
- * Returns null if the API client is not available or the query did not complete
- * successfully.
- * This method MUST be called off the main UI thread, as it will block until data is returned
- * from the API, which may include a network request.
- *
- * @param constraint Autocomplete query string
- * @return Results from the autocomplete API or null if the query was not successful.
- * @see GeoDataClient#getAutocompletePredictions(String, LatLngBounds, AutocompleteFilter)
- * @see AutocompletePrediction#freeze()
- */
- private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
- Log.i(TAG, "Starting autocomplete query for: " + constraint);
- // Submit the query to the autocomplete API and retrieve a PendingResult that will
- // contain the results when the query completes.
- Task<AutocompletePredictionBufferResponse> results =
- mGeoDataClient.getAutocompletePredictions(constraint.toString(), mBounds,
- mPlaceFilter);
- // This method should have been called off the main UI thread. Block and wait for at most
- // 60s for a result from the API.
- try {
- Tasks.await(results, 60, TimeUnit.SECONDS);
- } catch (ExecutionException | InterruptedException | TimeoutException e) {
- e.printStackTrace();
- }
- try {
- AutocompletePredictionBufferResponse autocompletePredictions = results.getResult();
- Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
- + " predictions.");
- // Freeze the results immutable representation that can be stored safely.
- return DataBufferUtils.freezeAndClose(autocompletePredictions);
- } catch (RuntimeExecutionException e) {
- // If the query did not complete successfully return null
- Toast.makeText(getContext(), "Error contacting API: " + e.toString(),
- Toast.LENGTH_SHORT).show();
- Log.e(TAG, "Error getting autocomplete prediction API call", e);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement