将长整型转换为整数



我在Github上使用YoutubePlaylist-Master库来制作我的Android应用程序。我只想拥有频道的最后一个视频,我尝试修改代码,但我有一个问题:

当我将值设置为 1 时,我有一个错误,因为我使用的是 Interger 而不是 Long。

public abstract class GetPlaylistAsyncTask extends AsyncTask<String, Void, Pair<String, List<Video>>> {
    private static final String TAG = "GetPlaylistAsyncTask";
    private static final Long YOUTUBE_PLAYLIST_MAX_RESULTS = 1L;
    private static final Integer YOUTUBE_LAST_V = 1;
    //see: https://developers.google.com/youtube/v3/docs/playlistItems/list
    private static final String YOUTUBE_PLAYLIST_PART = "snippet";
    private static final String YOUTUBE_PLAYLIST_FIELDS = "pageInfo,nextPageToken,items(id,snippet(resourceId/videoId))";
    //see: https://developers.google.com/youtube/v3/docs/videos/list
    private static final String YOUTUBE_VIDEOS_PART = "snippet,contentDetails,statistics"; // video resource properties that the response will include.
    private static final String YOUTUBE_VIDEOS_FIELDS = "items(id,snippet(title,description,thumbnails/high),contentDetails/duration,statistics)"; // selector specifying which fields to include in a partial response.
        (...)
    @Override
    protected Pair<String, List<Video>> doInBackground(String... params) {
        final String playlistId = params[0];
        final String nextPageToken;
        if (params.length == 2) {
            nextPageToken = params[1];
        } else {
            nextPageToken = null;
        }
        PlaylistItemListResponse playlistItemListResponse;
        try {
            playlistItemListResponse = mYouTubeDataApi.playlistItems()
                    .list(YOUTUBE_PLAYLIST_PART)
                    .setPlaylistId(playlistId)
                    .setPageToken(nextPageToken)
                    .setFields(YOUTUBE_PLAYLIST_FIELDS)
                    .setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS)
                    .setKey(ApiKey.YOUTUBE_API_KEY)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        if (playlistItemListResponse == null) {
            Log.e(TAG, "Failed to get playlist");
            return null;
        }
        List<String> videoIds = new ArrayList();
        // pull out the video id's from the playlist page
        for (PlaylistItem item : playlistItemListResponse.getItems()) {
            videoIds.add(item.getSnippet().getResourceId().getVideoId());
        }
        // get details of the videos on this playlist page
        VideoListResponse videoListResponse = null;
        try {
            videoListResponse = mYouTubeDataApi.videos()
                    .list(YOUTUBE_VIDEOS_PART)
                    .setFields(YOUTUBE_VIDEOS_FIELDS)
                    .setKey(ApiKey.YOUTUBE_API_KEY)
                    .setId(TextUtils.join(",", videoIds)).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Pair(playlistItemListResponse.getNextPageToken(), videoListResponse.getItems());
    }
}

所以问题是:

我有 2 个 VAR :

private static final Long YOUTUBE_PLAYLIST_MAX_RESULTS = 1L;
private static final Integer YOUTUBE_LAST_V = 1;

protected Pair<String, List<Video>> doInBackground(String... params)类中,当我尝试将.setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS)更改为.setMaxResults(YOUTUBE_LAST_V)时,出现错误:Error:(70, 36) error: incompatible types: Integer cannot be converted to Long

setMaxResults()不接受

int类型数据,因此YOUTUBE_PLAYLIST_MAX_RESULTS类型更改为long(后缀L(,如下所示:

private static final Long YOUTUBE_LAST_V = 1L;

只需设置

private static final Integer YOUTUBE_PLAYLIST_MAX_RESULTS = 1;

这是一个静态的最终字段,所以它永远不会改变,Integer就足够了......

最新更新