地方信息API有时无法显示正确的照片

时间:2019-05-30 02:44:39

标签: android google-places-api

我有一个可单击的回收者视图和一个sharedView UI,当我在它们之间切换时,图像并不总是正确的。

我正在使用Google API向特定数量的俱乐部获取信息:我有一个包含所有地点ID的数组。我在两个地方使用数组。我用它来加载照片的recyclerview,并在单击卡片并转换为新版式时再次加载。有时候,过渡时我看不到正确的图片。所有图片都来自一个数组,我不知道为什么它会改变顺序。我唯一想做的就是标记卡的位置。我单击了,系统正确将其拾取

 String[] clubs = {"ChIJO_uSYKNZwokRAC7RLeB0oZ8", "ChIJAQBEylJYwokRLbnrAchQImk",
        "ChIJU_26rfpYwokRTNf2K1-7p8E", "ChIJ38hxfnhZwokRx1HSFLj790w", "ChIJBwnlGrdZwokRpf61pMm860c"
        , "ChIJpSIzqrhZwokR1KnVMoVty_g", "ChIJMRV7375ZwokRAfltF6Y-wYw", "ChIJYabdHPhYwokRPmAV8GtM3gs",
        "ChIJi2dSjQRZwokRuXUKcv4riVc", "ChIJKaKVI79ZwokRN8WicODOIAw", "ChIJwXI8Fb5ZwokRr4JjG4HxSP8",
        "ChIJ6bU_E4ZZwokR2ZDbY_IhhrI"};

 @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.menu_venue, container, false);

    RecyclerView vRV = view.findViewById(R.id.view_venue);
    if (!Places.isInitialized()) {
        Places.initialize(contextForPlaces, "AIzaSyCKGd3fqmtsDklRGMhnkuIy1GS-j6gRBh8");
    }
    placesClient = Places.createClient(contextForPlaces);

    vRV.setHasFixedSize(true);
    RecyclerView.LayoutManager vLM = new LinearLayoutManager(this.getActivity());
    vAdapter = new VenueAdapter(vIL);
    vAdapter.setOnItemClickListener(this::shareTheView);



    for (String club : clubs) {
        FetchPlaceRequest request = FetchPlaceRequest.newInstance(club, placeFields);
        placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
            Place place = response.getPlace();
            PhotoMetadata photoMetadata = Objects.requireNonNull(place.getPhotoMetadatas()).get(0);

            FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata).setMaxHeight(200).setMaxWidth(200).build();
            placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {

                Bitmap bitmap = fetchPhotoResponse.getBitmap();

                vIL.add(new VenueItem(/*Photo*/bitmap, place.getName()));


                Log.i(TAG, "Photo Should Be Up");
                vRV.setLayoutManager(vLM);
                vRV.setAdapter(vAdapter);
            }).addOnFailureListener((exception) -> {
                if (exception instanceof ApiException) {
                    ApiException apiException = (ApiException) exception;
                    int statusCode = apiException.getStatusCode();
                    // Handle error with given status code.
                    Log.e(TAG, "Place not found: " + exception.getMessage());
                }
            });

            Log.i(TAG, "Place found: " + place.getName());
        }).addOnFailureListener((exception) -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                int statusCode = apiException.getStatusCode();
                // Handle error with given status code.
                Log.e(TAG, "Place not found: " + exception.getMessage());
            }
        });
    }

    return view;
}

public void shareTheView(int position, View sharedView) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    FetchPlaceRequest oneRequest = FetchPlaceRequest.newInstance(clubs[position], placeFields);
    placesClient.fetchPlace(oneRequest).addOnSuccessListener((response) -> {
        Place onePlace = response.getPlace();
        PhotoMetadata onePhotoMetadata = Objects.requireNonNull(onePlace.getPhotoMetadatas()).get(0);
        //String attributions = photoMetadata.getAttributions();

        FetchPhotoRequest onePhotoRequest = FetchPhotoRequest.builder(onePhotoMetadata).setMaxHeight(200).setMaxWidth(200).build();
        placesClient.fetchPhoto(onePhotoRequest).addOnSuccessListener((fetchPhotoResponse) -> {

            Bitmap venueBitmap = fetchPhotoResponse.getBitmap();
            venueBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();


            venueSharedIntent = new Intent(contextForPlaces, SharedVenueItem.class);
            //venueSharedIntent.putExtra("Venue_Position", position);
            venueSharedIntent.putExtra("Place_Photo", byteArray);
            venueSharedIntent.putExtra("Place_Name", onePlace.getName());
            venueSharedIntent.putExtra("Place_Address", onePlace.getAddress());

            if ((onePlace.getOpeningHours()) == null)
                venueSharedIntent.putExtra("Place_Hours", "There are no hours availiable for this location");
            else
                venueSharedIntent.putExtra("Place_Hours", onePlace.getOpeningHours().getWeekdayText().toString());

            ActivityOptionsCompat optionsCompat =
                    ActivityOptionsCompat.makeSceneTransitionAnimation(Objects.requireNonNull(getActivity()),
                            sharedView, Objects.requireNonNull(ViewCompat.getTransitionName(sharedView)));
            startActivity(venueSharedIntent, optionsCompat.toBundle());
            Log.i(TAG, "Photo Should Be Up");

        }).addOnFailureListener((exception) -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                int statusCode = apiException.getStatusCode();
                Log.e(TAG, "Place not found: " + exception.getMessage());
            }
        });

        Log.i(TAG, "Place found: " + onePlace.getName());
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            ApiException apiException = (ApiException) exception;
            int statusCode = apiException.getStatusCode();
            Log.e(TAG, "Place not found: " + exception.getMessage());
        }
    });
}

我需要在recyclerview卡中单击的照片与我每次从“共享视图方法”中获得的照片匹配

0 个答案:

没有答案