Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. private static final String TAG = "PlaceAutocompleteAd";
  2. private static final CharacterStyle STYLE_BOLD = new StyleSpan(Typeface.BOLD);
  3. /**
  4. * Current results returned by this adapter.
  5. */
  6. private ArrayList<AutocompletePrediction> mResultList;
  7.  
  8. /**
  9. * Handles autocomplete requests.
  10. */
  11. private GeoDataClient mGeoDataClient;
  12.  
  13. /**
  14. * The bounds used for Places Geo Data autocomplete API requests.
  15. */
  16. private LatLngBounds mBounds;
  17.  
  18. /**
  19. * The autocomplete filter used to restrict queries to a specific set of place types.
  20. */
  21. private AutocompleteFilter mPlaceFilter;
  22.  
  23. /**
  24. * Initializes with a resource for text rows and autocomplete query bounds.
  25. *
  26. * @see android.widget.ArrayAdapter#ArrayAdapter(android.content.Context, int)
  27. */
  28. public PlaceAutocompleteAdapter(Context context, GeoDataClient geoDataClient,
  29. LatLngBounds bounds, AutocompleteFilter filter) {
  30. super(context, android.R.layout.simple_expandable_list_item_2, android.R.id.text1);
  31. mGeoDataClient = geoDataClient;
  32. mBounds = bounds;
  33. mPlaceFilter = filter;
  34. }
  35.  
  36. /**
  37. * Sets the bounds for all subsequent queries.
  38. */
  39. public void setBounds(LatLngBounds bounds) {
  40. mBounds = bounds;
  41. }
  42.  
  43. /**
  44. * Returns the number of results received in the last autocomplete query.
  45. */
  46. @Override
  47. public int getCount() {
  48. return mResultList.size();
  49. }
  50.  
  51. /**
  52. * Returns an item from the last autocomplete query.
  53. */
  54. @Override
  55. public AutocompletePrediction getItem(int position) {
  56. return mResultList.get(position);
  57. }
  58.  
  59. @Override
  60. public View getView(int position, View convertView, ViewGroup parent) {
  61. View row = super.getView(position, convertView, parent);
  62.  
  63. // Sets the primary and secondary text for a row.
  64. // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
  65. // styling based on the given CharacterStyle.
  66.  
  67. AutocompletePrediction item = getItem(position);
  68.  
  69. TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
  70. TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
  71. textView1.setText(item.getPrimaryText(STYLE_BOLD));
  72. textView2.setText(item.getSecondaryText(STYLE_BOLD));
  73.  
  74. return row;
  75. }
  76.  
  77. /**
  78. * Returns the filter for the current set of autocomplete results.
  79. */
  80. @Override
  81. public Filter getFilter() {
  82. return new Filter() {
  83. @Override
  84. protected FilterResults performFiltering(CharSequence constraint) {
  85. FilterResults results = new FilterResults();
  86.  
  87. // We need a separate list to store the results, since
  88. // this is run asynchronously.
  89. ArrayList<AutocompletePrediction> filterData = new ArrayList<>();
  90.  
  91. // Skip the autocomplete query if no constraints are given.
  92. if (constraint != null) {
  93. // Query the autocomplete API for the (constraint) search string.
  94. filterData = getAutocomplete(constraint);
  95. }
  96.  
  97. results.values = filterData;
  98. if (filterData != null) {
  99. results.count = filterData.size();
  100. } else {
  101. results.count = 0;
  102. }
  103.  
  104. return results;
  105. }
  106.  
  107. @Override
  108. protected void publishResults(CharSequence constraint, FilterResults results) {
  109.  
  110. if (results != null && results.count > 0) {
  111. // The API returned at least one result, update the data.
  112. mResultList = (ArrayList<AutocompletePrediction>) results.values;
  113. notifyDataSetChanged();
  114. } else {
  115. // The API did not return any results, invalidate the data set.
  116. notifyDataSetInvalidated();
  117. }
  118. }
  119.  
  120. @Override
  121. public CharSequence convertResultToString(Object resultValue) {
  122. // Override this method to display a readable result in the AutocompleteTextView
  123. // when clicked.
  124. if (resultValue instanceof AutocompletePrediction) {
  125. return ((AutocompletePrediction) resultValue).getFullText(null);
  126. } else {
  127. return super.convertResultToString(resultValue);
  128. }
  129. }
  130. };
  131. }
  132.  
  133. /**
  134. * Submits an autocomplete query to the Places Geo Data Autocomplete API.
  135. * Results are returned as frozen AutocompletePrediction objects, ready to be cached.
  136. * Returns an empty list if no results were found.
  137. * Returns null if the API client is not available or the query did not complete
  138. * successfully.
  139. * This method MUST be called off the main UI thread, as it will block until data is returned
  140. * from the API, which may include a network request.
  141. *
  142. * @param constraint Autocomplete query string
  143. * @return Results from the autocomplete API or null if the query was not successful.
  144. * @see GeoDataClient#getAutocompletePredictions(String, LatLngBounds, AutocompleteFilter)
  145. * @see AutocompletePrediction#freeze()
  146. */
  147. private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
  148. Log.i(TAG, "Starting autocomplete query for: " + constraint);
  149.  
  150. // Submit the query to the autocomplete API and retrieve a PendingResult that will
  151. // contain the results when the query completes.
  152. Task<AutocompletePredictionBufferResponse> results =
  153. mGeoDataClient.getAutocompletePredictions(constraint.toString(), mBounds,
  154. mPlaceFilter);
  155.  
  156. // This method should have been called off the main UI thread. Block and wait for at most
  157. // 60s for a result from the API.
  158. try {
  159. Tasks.await(results, 60, TimeUnit.SECONDS);
  160. } catch (ExecutionException | InterruptedException | TimeoutException e) {
  161. e.printStackTrace();
  162. }
  163.  
  164. try {
  165. AutocompletePredictionBufferResponse autocompletePredictions = results.getResult();
  166.  
  167. Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
  168. + " predictions.");
  169.  
  170. // Freeze the results immutable representation that can be stored safely.
  171. return DataBufferUtils.freezeAndClose(autocompletePredictions);
  172. } catch (RuntimeExecutionException e) {
  173. // If the query did not complete successfully return null
  174. Toast.makeText(getContext(), "Error contacting API: " + e.toString(),
  175. Toast.LENGTH_SHORT).show();
  176. Log.e(TAG, "Error getting autocomplete prediction API call", e);
  177. return null;
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement