Android Place Autocomplete无法正常工作

Android Place Autocomplete无法正常工作,android,api,maps,Android,Api,Maps,我正在我的项目中使用PlaceAutoComplete新sdk 实现'com.google.android.libraries.places:places:2.1.0' Autocompletetextview:我用它来显示数据。问题是 它第一次而不是第二次正常工作。列表不会显示在屏幕上 第二次和某人的地址。有时我会出错 :PlaceAutoCompleteAdapter:未找到位置:9010。 请帮助解决这个问题 公共类placeAutoCompletedAdapterNew扩展ArrayAd

我正在我的项目中使用PlaceAutoComplete新sdk

实现'com.google.android.libraries.places:places:2.1.0'

Autocompletetextview:我用它来显示数据。问题是 它第一次而不是第二次正常工作。列表不会显示在屏幕上 第二次和某人的地址。有时我会出错 :PlaceAutoCompleteAdapter:未找到位置:9010。 请帮助解决这个问题

公共类placeAutoCompletedAdapterNew扩展ArrayAdapter实现可过滤{
地点客户地点客户;
自动完成会话令牌;
private static final CharacterStyle_BOLD=新样式span(Typeface.BOLD);
私有列表mResultList;
私有列表结果;
语境;
私有字符串TAG=“PlaceAutoCompleteAdapter”;
public PlaceAutoCompleteApterNew(上下文上下文、PlacesClient PlacesClient、AutocompleteSessionToken令牌){
super(context,android.R.layout.simple\u expandable\u list\u item\u 1,android.R.id.text1);
this.context=上下文;
this.placesClient=placesClient;
this.token=token;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=super.getView(位置、转换视图、父级);
自动完成预测项=获取项(位置);
TextView textView1=(TextView)row.findViewById(android.R.id.text1);
textView1.setText(item.getPrimaryText(STYLE_BOLD));
返回行;
}
@凌驾
public int getCount(){
if(mResultList==null)
返回0;
其他的
返回mResultList.size();
}
@凌驾
公共自动完成预测getItem(int位置){
返回mResultList.get(位置);
}
@凌驾
公共过滤器getFilter(){
返回新筛选器(){
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults results=新的FilterResults();
//如果未给出任何约束,则跳过自动完成查询。
if(约束!=null){
//查询(约束)搜索字符串的自动完成API。
mResultList=getAutoComplete(约束);
if(mResultList!=null){
//API已成功返回结果。
results.values=mResultList;
results.count=mResultList.size();
}
}
返回结果;
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
if(results!=null&&results.count>0){
//API返回了至少一个结果,请更新数据。
notifyDataSetChanged();
}否则{
//API未返回任何结果,因此数据集无效。
notifyDataSetionValidated();
}
}
@凌驾
public CharSequence ConvertResultString(对象结果值){
//重写此方法以在AutocompleteTextView中显示可读结果
//单击时。
if(自动完成预测的结果值实例){
返回((自动完成预测)结果值).getFullText(空);
}否则{
返回super.convertResultToString(resultValue);
}
}
};
}
私有列表getAutoComplete(字符序列约束){
//为自动完成会话创建新令牌。将此令牌传递给FindAutocompletePredictionsRequest,
//当用户进行选择时(例如调用fetchPlace()时),再次执行此操作。
//AutocompleteSessionToken=AutocompleteSessionToken.newInstance();
//创建矩形边界对象。
//使用生成器创建FindAutocompletePredictionsRequest。
FindAutocompletePredictionsRequest=FindAutocompletePredictionsRequest.builder()
.setTypeFilter(TypeFilter.ADDRESS)
.setSessionToken(令牌)
.setQuery(constraint.toString())
.build();
placesClient.findAutocompletePredictions(请求).addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(FindAtoCompletePredictionsResponse){
例如(AutocompletePrediction预测:response.getAutocompletePredictions()){
Log.e(TAG,prediction.getPrimaryText(null.toString());
}
tempResult=response.getAutocompletePredictions();
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
if(ApiException的异常实例){
ApiException=(ApiException)异常;
Log.e(标记“未找到位置:”+apiException.getStatusCode());
}
}
});
返回结果;
}
}

错误代码表明places api计费存在问题,代码9010表示您超出了计费允许的查询限制。 Places autocomplete不是免费的,但Google允许您在拒绝请求之前,在不设置计费帐户的情况下尝试几次。

My billing
public class PlaceAutocompleteAdapterNew extends ArrayAdapter<AutocompletePrediction> implements Filterable {
        PlacesClient placesClient;
        AutocompleteSessionToken token;
        private static final CharacterStyle STYLE_BOLD = new StyleSpan(Typeface.BOLD);
        private List<AutocompletePrediction> mResultList;
        private List<AutocompletePrediction> tempResult;
        Context context;
        private String TAG = "PlaceAutoCompleteAdapter";

        public PlaceAutocompleteAdapterNew(Context context, PlacesClient placesClient, AutocompleteSessionToken token) {
            super(context, android.R.layout.simple_expandable_list_item_1, android.R.id.text1);
            this.context = context;
            this.placesClient = placesClient;
            this.token = token;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row = super.getView(position, convertView, parent);

            AutocompletePrediction item = getItem(position);

            TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
            textView1.setText(item.getPrimaryText(STYLE_BOLD));
            return row;
        }

        @Override
        public int getCount() {
            if (mResultList == null)
                return 0;
            else
                return mResultList.size();
        }

        @Override
        public AutocompletePrediction getItem(int position) {
            return mResultList.get(position);
        }

        @Override
        public Filter getFilter() {
            return new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults results = new FilterResults();
                    // Skip the autocomplete query if no constraints are given.
                    if (constraint != null) {
                        // 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();
                    }
                }

                @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);
                    }
                }
            };
        }

        private List<AutocompletePrediction> getAutoComplete(CharSequence constraint) {
            // Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
            // and once again when the user makes a selection (for example when calling fetchPlace()).
            // AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
            // Create a RectangularBounds object.

            // Use the builder to create a FindAutocompletePredictionsRequest.
            FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
                    .setTypeFilter(TypeFilter.ADDRESS)
                    .setSessionToken(token)
                    .setQuery(constraint.toString())
                    .build();

            placesClient.findAutocompletePredictions(request).addOnSuccessListener(new OnSuccessListener<FindAutocompletePredictionsResponse>() {
                @Override
                public void onSuccess(FindAutocompletePredictionsResponse response) {
                    for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
                        Log.e(TAG, prediction.getPrimaryText(null).toString());
                    }
                    tempResult = response.getAutocompletePredictions();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    if (exception instanceof ApiException) {
                        ApiException apiException = (ApiException) exception;
                        Log.e(TAG, "Place not found: " + apiException.getStatusCode());
                    }
                }
            });
            return tempResult;
        }
    }