0

チュートリアルの1つにこのPlacesAutoCompleteAdapterが見つかりましたが、指定したデータを表示するように変更することはできません。PlacesAutoCompleteAdapterを編集して選択した都市のみを保存します

私がしたい:AutoCompleteTextViewに保存

  • 表示のみの都市(すべてではない企業やなど)
  • 項目がリスト

から選択された場合にのみ、あなたたちはこれで私を助けることができます? 私は私だけcitites(localityまたはadministrative_area_level_3)を表示するmPlaceFilterを変更する必要があると思うが、私は方法がわからない...

public class PlacesAutoCompleteAdapter 
     extends ArrayAdapter<PlacesAutoCompleteAdapter.PlaceAutocomplete> implements Filterable { 

    private static final String TAG = "PlaceAutocomplete"; 
    /** 
    * Current results returned by this adapter. 
    */ 
    private ArrayList<PlaceAutocomplete> mResultList; 

    /** 
    * Handles autocomplete requests. 
    */ 
    private GoogleApiClient mGoogleApiClient; 

    /** 
    * 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 ArrayAdapter#ArrayAdapter(Context, int) 
    */ 
    public PlacesAutoCompleteAdapter(Context context, int resource, GoogleApiClient googleApiClient, 
            LatLngBounds bounds, AutocompleteFilter filter) { 
     super(context, resource); 
     mGoogleApiClient = googleApiClient; 
     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 PlaceAutocomplete getItem(int position) { 
     return mResultList.get(position); 
    } 

    /** 
    * Returns the filter for the current set of autocomplete results. 
    */ 
    @Override 
    public Filter getFilter() { 
     Filter filter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       FilterResults results = new FilterResults(); 
       // Skip the autocomplete query if no constraints are given OR has got less than 3 letters 
       if (constraint != null && constraint.length() >= 3) { 
        // Query the autocomplete API for the (constraint) search string. 
        mResultList = getAutocomplete(constraint); 
        if (mResultList != null) { 
         // The API successfully returned results. 
         results.values = mResultList; 
         results.count = mResultList.size(); 
        } 
       } 
       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. 
        notifyDataSetChanged(); 
       } else { 
        // The API did not return any results, invalidate the data set. 
        notifyDataSetInvalidated(); 
       } 
      } 
     }; 
     return filter; 
    } 

    private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) { 
     if (mGoogleApiClient.isConnected()) { 
      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. 
      PendingResult<AutocompletePredictionBuffer> results = 
        Places.GeoDataApi 
          .getAutocompletePredictions(mGoogleApiClient, 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. 
      AutocompletePredictionBuffer autocompletePredictions = results 
        .await(60, TimeUnit.SECONDS); 

      // Confirm that the query completed successfully, otherwise return null 
      final Status status = autocompletePredictions.getStatus(); 
      if (!status.isSuccess()) { 
       Toast.makeText(getContext(), "Error contacting API: " + status.getStatusMessage(), 
         Toast.LENGTH_SHORT).show(); 
       Log.e(TAG, "Error getting autocomplete prediction API call: " + status.toString()); 
       autocompletePredictions.release(); 
       return null; 
      } 

      Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount() 
        + " predictions."); 

      // Copy the results into our own data structure, because we can't hold onto the buffer. 
      // AutocompletePrediction objects encapsulate the API response (place ID and description). 

      Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator(); 
      ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount()); 
      while (iterator.hasNext()) { 
       AutocompletePrediction prediction = iterator.next(); 
       // Get the details of this prediction and copy it into a new PlaceAutocomplete object. 
       resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getFullText(null))); 
      } 

      // Release the buffer now that all data has been copied. 
      autocompletePredictions.release(); 

      return resultList; 
     } 
     Log.e(TAG, "Google API client is not connected for autocomplete query."); 
     return null; 
    } 

    /** 
    * Holder for Places Geo Data Autocomplete API results. 
    */ 
    public class PlaceAutocomplete { 

     public CharSequence placeId; 
     public CharSequence description; 

     PlaceAutocomplete(CharSequence placeId, CharSequence description) { 
      this.placeId = placeId; 
      this.description = description; 
     } 

     @Override 
     public String toString() { 
      return description.toString(); 
     } 
    } 
} 

答えて

0

このような都市に設定フィルタ:

mPlaceFilter = new AutocompleteFilter.Builder() 
     .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES) 
     .build(); 
関連する問題