我正在开发一个向用户显示其所在地区不同场所的应用程序,为此,我正在使用google place API。我将所有结果存储在ArrayList中,但是由于某些原因,将它们添加到ArrayList的代码未运行。知道可能是什么原因吗?作为参考,我的代码的相关部分如下:
private void handleAutoCompleteRequest(FindAutocompletePredictionsRequest request) {
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);
placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
Log.d("PLACEID", prediction.getPlaceId());
Log.d("PLACENAME", prediction.getPrimaryText(null).toString());
placeID = prediction.getPlaceId();
placeName = prediction.getPrimaryText(null).toString();
// Define a Place ID.
final String placeId = placeID;
// Specify the fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.BUSINESS_STATUS, Place.Field.RATING, Place.Field.PHOTO_METADATAS);
// Construct a request object, passing the place ID and fields array.
final FetchPlaceRequest request1 = FetchPlaceRequest.newInstance(placeId, placeFields);
placesClient.fetchPlace(request1).addOnSuccessListener((response1) -> {
Place place = response1.getPlace();
Log.d("name", "Place found: " + place.getName());
Log.d("address", "Place found: " + place.getAddress());
Log.d("hours", "Place found: " + place.getBusinessStatus());
String statusString = String.valueOf(place.getBusinessStatus());
String ratingString = String.valueOf(place.getRating());
Log.d("placeStatus",statusString);
Log.d("placeRating",ratingString);
//PHOTOS///////////////////////////////////////////
// Get the photo metadata.
final List<PhotoMetadata> metadata = place.getPhotoMetadatas();
if (metadata == null || metadata.isEmpty()) {
Log.w("metadataNull", "No photo metadata.");
return;
}
final PhotoMetadata photoMetadata = metadata.get(0);
// Get the attribution text.
final String attributions = photoMetadata.getAttributions();
// Create a FetchPhotoRequest.
final FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata)
.setMaxWidth(500) // Optional.
.setMaxHeight(300) // Optional.
.build();
placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
placePhoto = fetchPhotoResponse.getBitmap();
Log.d("isthisrunning","yesitsrunning");
TenderSuggestion mySuggestion = new TenderSuggestion(place.getAddress(), place.getName(), place.getId(), statusString, ratingString, placePhoto);
TenderSuggestions.add(mySuggestion);
Log.d("testArrayList", "added");
for (int i = 0; i < TenderSuggestions.size(); i++) {
String master = TenderSuggestions.get(i).getSuggestionName();
for (int p = 0; p < TenderSuggestions.size(); p++) {
if (p != i) {
String subject = TenderSuggestions.get(p).getSuggestionName();
if (master.equals(subject)) {
TenderSuggestions.remove(p);
i--;
}
}
}
}
NUM_PAGES = TenderSuggestions.size();
Log.d("Arraylength1",String.valueOf(TenderSuggestions.size()));
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
final ApiException apiException = (ApiException) exception;
Log.e("placeError", "Place not found: " + exception.getMessage());
final int statusCode = apiException.getStatusCode();
// TODO: Handle error with given status code.
}
});
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
final ApiException apiException = (ApiException) exception;
Log.d("error", "Place not found: " + exception.getMessage());
final int statusCode = apiException.getStatusCode();
// TODO: Handle error with given status code.
}
Log.d("error", "Place not found: " + exception.getMessage());
});
}
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.d("ERROR", "Place not found: " + apiException.getStatusCode());
}
});
}