javayoutube。搜索列表搜索不返回任何内容
我正在开发一个应用程序,它实现了一个SearchView,用于通过关键字获取YouTube视频结果。然后通过缩略图url、视频标题、频道名称和视频视图对结果进行细化,其值存储在ArrayList中。ArrayList然后向java类发送值,java类将这些结果放入ListView中,为用户生成“结果”,就像YouTube应用程序返回视频一样。我使用的是YouTube数据API V3,并且使用的是谷歌示例代码中稍加修改的代码。这是我的searchview侦听器代码
searchedList是我要将字符串值发送到的ArrayList
searchYouTube = (SearchView)findViewById(R.id.videoSearch);
searchYouTube.setQueryHint("Search for a YouTube video");
searchYouTube.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String s) {
String query = searchYouTube.getQuery().toString();
searchedList.clear();
try {
new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("myApp").build();
// Define the API request for retrieving search results.
YouTube.Search.List search = youtube.search().list("id,snippet");
// Set your developer key from the Google Cloud Console for
// non-authenticated requests. See:
// https://cloud.google.com/console
search.setKey(apiKey);
search.setQ(query);
// Restrict the search results to only include videos. See:
// https://developers.google.com/youtube/v3/docs/search/list#type
search.setType("video");
// To increase efficiency, only retrieve the fields that the
// application uses.
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/channelTitle,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
if (searchResultList != null) {
apppendToSearchArray(searchResultList.iterator(), query);
}
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
下面是我的方法'appendToSearchArray'方法,在上面的代码中调用该方法以实际将所需的值按顺序放入ArrayList
private static void apppendToSearchArray(Iterator<SearchResult> iteratorSearchResults, String query) {
if (!iteratorSearchResults.hasNext()) {
System.out.println(" There aren't any results for your query.");
}
while (iteratorSearchResults.hasNext()) {
SearchResult singleVideo = iteratorSearchResults.next();
ResourceId rId = singleVideo.getId();
// Confirm that the result represents a video. Otherwise, the
// item will not contain a video ID.
if (rId.getKind().equals("youtube#video")) {
Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault();
VideoDetails singleVidDetails = new VideoDetails(thumbnail.getUrl().toString(),singleVideo.getSnippet().getTitle().toString(),singleVideo.getSnippet().getChannelTitle().toString(),"0");
searchedList.add(singleVidDetails);
}
}
}
“VideoDetails”是我创建的一个java类,它实际上充当了检索缩略图、标题、频道和视图的字符串值的媒介,并将这些字符串发送到适配器类,适配器类实际将它们实现到我的ListView中。但是,当应用程序运行并且我通过SearchView提交查询时,listview不会显示任何新结果。这是我的日志
非常感谢您的帮助,因为我很困惑
共 (0) 个答案