从一个位置/照片API获取到回收即可添加的项目,并且视图为空



我一直在开发一个应用程序,该应用程序需要我从Google Place API中获取诸如place name和Place Photo之类的信息并将其设置为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适配器的一部分。我曾经是图像的促进,因为图像来自可绘制的文件夹

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

我还必须从原始物品中更改的项目本身。我把字符串变成了一个位置,int是一个位图。我认为那会起作用。

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

我希望能够使用PloceClient请求地点名称和该地点的照片,并在Recyclerview的for中进行预处理。我知道该地点ID是正确的,因为日志返回所有位置的名称。但是它们没有出现在Recyclerview

我自己想出了答案。

vrv.setlayoutmanager(vlm); vrv.setadapter(vadapter);

我只需要将这两条线放入列表项下的for循环中,以便每个项目都可以在Meing删除

之前逃脱

最新更新