问题描述
我创建了一个令牌(在创建活动时)并将其保存在另一个类(单例)中。
我在适当的位置分配了令牌,如文档中所示。
但是会话不起作用。当我在搜索字段中输入 8 个字母时,Places API 请求的数量增加了 8 个。
应该是1。
错误一定在适配器本身的某个地方,因为即使我没有点击列表中的任何项目,它也会立即将每个字符计数为 1 个请求。
UtilsWayo.searchToken = AutocompleteSessionToken.newInstance()
AutoCompleteAdapter
public class AutoCompleteAdapter extends ArrayAdapter<AutocompletePrediction> implements Filterable {
private List<AutocompletePrediction> mResultList;
private PlacesClient placesClient;
AutoCompleteAdapter(Context context,PlacesClient placesClient) {
super(context,android.R.layout.simple_expandable_list_item_2,android.R.id.text1);
this.placesClient = placesClient;
}
@Override
public int getCount() {
return mResultList.size();
}
@Override
public AutocompletePrediction getItem(int position) {
return mResultList.get(position);
}
@NonNull
@Override
public View getView(int position,View convertView,@NonNull ViewGroup parent) {
View row = super.getView(position,convertView,parent);
row.setPadding(50,0);
AutocompletePrediction item = getItem(position);
parent.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border_out_bottom_left_right));
TextView textView1 = row.findViewById(android.R.id.text1);
TextView textView2 = row.findViewById(android.R.id.text2);
if (item != null) {
textView1.setText(item.getPrimaryText( null));
textView1.setTypeface(Typeface.DEFAULT_BOLD);
textView1.setTextColor(Color.parseColor("#32A3F0"));
textView2.setText(item.getSecondaryText(null));
textView2.setTextColor(Color.parseColor("#32A3F0"));
}
return row;
}
@NonNull
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
// We need a separate list to store the results,since
// this is run asynchronously.
List<AutocompletePrediction> filterData = new ArrayList<>();
// Skip the autocomplete query if no constraints are given.
if (charSequence != null) {
// Query the autocomplete API for the (constraint) search string.
filterData = getAutocomplete(charSequence);
}
results.values = filterData;
if (filterData != null) {
results.count = filterData.size();
} else {
results.count = 0;
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence charSequence,FilterResults results) {
try {
if (results != null && results.count > 0) {
// The API returned at least one result,update the data.
mResultList = (List<AutocompletePrediction>) results.values;
notifyDataSetChanged();
} else {
// The API did not return any results,invalidate the data set.
notifyDataSetInvalidated();
}
} catch (Exception e) {
e.printstacktrace();
}
}
@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 RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
new LatLng(18.62556 - 0.1,54.380443 + 0.1),new LatLng(18.62556 + 0.1,54.38044 + 0.1)
);
final FindAutocompletePredictionsRequest requestBuilder =
FindAutocompletePredictionsRequest.builder()
.setorigin(new LatLng(18.62556,54.38044))
.setCountry("PL")
.setLocationBias(bounds)
.setTypeFilter(TypeFilter.ADDRESS)
.setSessionToken(UtilsWayo.searchToken)
.setQuery(constraint.toString())
.build();
Task<FindAutocompletePredictionsResponse> results =
placesClient.findAutocompletePredictions(requestBuilder);
//Wait to get results.
try {
Tasks.await(results,60,TimeUnit.SECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
e.printstacktrace();
}
if (results.isSuccessful()) {
if (results.getResult() != null) {
Log.d("SESSION TOKEN IN REQUE ",requestBuilder.getSessionToken().toString());
Log.d("SESSION ADAPTER XXX",results.getResult().getAutocompletePredictions().toString());
return results.getResult().getAutocompletePredictions();
}
return null;
} else {
return null;
}
}
}
活动
private val autocompleteClickListener =
OnItemClickListener { adapterView,view,i,l ->
try {
val item = adapter!!.getItem(i)
var placeID: String? = null
if (item != null) {
placeID = item.placeId
}
// To specify which data types to return,pass an array of Place.Fields in your FetchPlaceRequest
// Use only those fields which are required.
val placeFields = listof(
Place.Field.ID,Place.Field.NAME,Place.Field.ADDRESS,Place.Field.LAT_LNG
)
var request: FetchPlaceRequest? = null
if (placeID != null) {
request = FetchPlaceRequest.builder(placeID,placeFields)
.setSessionToken(UtilsWayo.searchToken)
.build()
}
if (request != null) {
placesClient!!.fetchPlace(request).addOnSuccessListener { task ->
UtilsWayo.searchToken = AutocompleteSessionToken.newInstance()
task.place.latLng?.let {
getDirections(UtilsWayo.currentLocation,it)
}
UtilsWayo.currentLocation = UtilsWayo.currentLocation
task.place.latLng?.let {
UtilsWayo.destinationLocation = it
}
val myBounds = LatLngBounds.Builder()
myBounds.include(task.place.latLng)
mMap.moveCamera(
CameraUpdateFactory.newLatLngBounds(
myBounds.build(),150
)
)
mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f))
// Marker celu
mMap.addMarker(
task.place.latLng?.let {
MarkerOptions()
.position(it)
}
)
}.addOnFailureListener { e ->
e.printstacktrace()
responseView.text = e.message
}
}
} catch (e: Exception) {
e.printstacktrace()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)