将地点/照片api提取中的项目添加到recyclerview并且视图为空

问题描述 投票:0回答:1

我一直在开发一个应用程序,要求我从google PLaces Api获取地名和PLace照片等信息并将其设置为RecyclerView。我被困了,因为我设法让代码工作没有错误,但RecyclerView是空的。我的代码出了什么问题?

我被卡住了,因为我不知道问题出在哪里。当我运行代码时,所有的提取都工作,标签显示在日志中,所以我完全迷失了。我的第一个想法是,我正在显示错误的代码,但后来我没有办法前进并将其改为别的东西,因为我不确定它是好还是坏。

This is the Fragment for the RecyclerView Item:

public class VenueList extends Fragment{
ArrayList<VenueItem> vIL = new ArrayList<>();
private PlacesClient placesClient;
private Context contextForPlaces;

我正在使用的地方的地方ID

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

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    contextForPlaces = context;
}

@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);
    List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.PHOTO_METADATAS);
    if (!Places.isInitialized()) {
        Places.initialize(contextForPlaces, "AIzaSyCKGd3fqmtsDklRGMhnkuIy1GS-j6gRBh8");}
    placesClient = Places.createClient(contextForPlaces);


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

    // run through each photo to make sure it has a place attached to it then insert each photo and place into the vIL
    //createBitmap for fetchPhoto
    for (String club : clubs) {
        FetchPlaceRequest request = FetchPlaceRequest.newInstance(club, placeFields);
        placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
            Place place = response.getPlace();
            PhotoMetadata photoMetadata = place.getPhotoMetadatas().get(0);
            String attributions = photoMetadata.getAttributions();
            FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata).setMaxHeight(200).build();
            placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
                Bitmap bitmap = fetchPhotoResponse.getBitmap();
                vIL.add(new VenueItem(/*Photo*/bitmap, place/*Name*/));
                Log.i(TAG, "Photo Should Be Up: ");

            }).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());
            }
        });


    }
    vRV.setLayoutManager(vLM);
    vRV.setAdapter(vAdapter);

    return view;
}

这是我改变的RecyclerView适配器的一部分。我曾经是图像的getResourse,因为图像来自drawable文件夹

public void onBindViewHolder(@NonNull VenueViewHolder venueViewHolder, int i) {
    VenueItem currentItem = vIAL.get(i);
    if(currentItem.getVenueImageResource() == null){
        venueViewHolder.vIV.setImageResource(R.drawable.ic_android);
    }else
    venueViewHolder.vIV.setImageBitmap(currentItem.getVenueImageResource());
    venueViewHolder.vTV.setText((CharSequence) currentItem.getVenueDescription());
}

物品本身,我也不得不改变它原来的一点。我将字符串设为Place,将int设为Bitmap。我认为这会奏效。

public class VenueItem {
private Bitmap venueImageResource;
private Place venueDescription;

public VenueItem(Bitmap vIR, Place description) {
    venueImageResource = vIR;
    venueDescription = description;
}

public Bitmap getVenueImageResource() {
    return venueImageResource;
}

public Place getVenueDescription() {
    return venueDescription;
}
}

我希望能够使用placesClient请求地名和照片,并将其放在RecyclerView的for中。我知道地方ID是正确的,因为日志返回所有地方的名称。但它们没有出现在RecyclerView上

google-maps android-recyclerview google-places
1个回答
0
投票

我自己想出了答案。

vRV.setLayoutManager(vLM); vRV.setAdapter(vAdapter);

我只需将这两行放入列表项下的for循环中,这样每个项都可以在meing擦除之前逃脱

© www.soinside.com 2019 - 2024. All rights reserved.