我正在尝试创建“收藏夹”课程。我搜索我从用户那里获得的标题,并将视频添加到收藏夹类。但是,我总是得到不止一个结果。 (我得到的最大结果是10)。 我怎样才能为每个标题获得1个结果?
这是on create:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_layout);
getSupportActionBar().setTitle("Favorites");
initializeViews();
extras = getIntent().getExtras();
this.vidTitle = extras.getString("title");
this.vidID = extras.getString("id");
//Checking where to add the new Video
for(int i=0;i<favorites.length;i++){
if(favorites[i]==null){
favorites[i] = vidTitle;
}
break; // Break so it won't add same video to all array
}
AppUtils.showToast("Loading Favorites");
getVideo2();
}
这是得到10个结果的代码:
public void getVideo(){
AppUtils.showToast("Loading Favorites");
mServiceTask = new ServiceTask(SEARCH_VIDEO);
mServiceTask.setmServerResponseListener(this);
for (int i=0; i<favorites.length;i++) {
if(favorites[i]!=null){
mServiceTask.execute(new Object[]{favorites[i]}); <--- Problem here
break;
}
else{
break;
}
}
}
ServiceTask:
public class ServiceTask extends AsyncTask<Object, Void, Object[]> implements ServiceTaskInterface{
private static final String TAG = ServiceTask.class.getSimpleName();
private ServerResponseListener mServerResponseListener = null;
private int mRequestCode = 0;
public void setmServerResponseListener(ServerResponseListener mServerResponseListener){
this.mServerResponseListener = mServerResponseListener;
}
public ServiceTask(int iReqCode){
mRequestCode = iReqCode;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mServerResponseListener.prepareRequest(mRequestCode);
}
@Override
protected Object[] doInBackground(Object... params) {
if(params == null)
throw new NullPointerException("Parameters to the async task can never be null");
mServerResponseListener.goBackground();
Object[] resultDetails = new Object[2];
resultDetails[0] = mRequestCode;
switch (mRequestCode){
case AppConstants.SEARCH_VIDEO:
try {
resultDetails[1] = loadVideos((String) params[0]);
break;
}catch (Exception e){
AppUtils.showToast("BLABLABLA");}
}
return resultDetails;
}
@Override
protected void onPostExecute(Object[] result) {
super.onPostExecute(result);
mServerResponseListener.completedRequest(result);
}
//Loading the videos, with help of Google's code.
private List<SearchResult> loadVideos(String queryTerm){
try{
YouTube youTube = new YouTube.Builder(transport,jsonFactory,new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {}
}).setApplicationName(YoutubeApplication.appName()).build();
YouTube.Search.List search = youTube.search().list("id,snippet");
search.setKey(AppConstants.KEY);
search.setQ(queryTerm);
//Only including videos
search.setType("video");
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/description,snippet/thumbnails/default/url,snippet/thumbnails/medium/url)");
search.setMaxResults(AppConstants.NUMBER_OF_VIDEOS_RETURNED);
//Call the API to print results
SearchListResponse searchListResponse = search.execute();
List<SearchResult> searchResultList = searchListResponse.getItems();
if(searchResultList != null){
return searchResultList;
}
}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 null;
}
}
我如何才能获得1个结果,但仍然可以获得10个结果(如10个不同的视频)?
答案 0 :(得分:0)
首先,评论您的def logTry[T](what: Try[T], block: T => String): Unit = {
what match {
case Success(res) => println(block(res))
case Failure(t) => println(???) // how to do this
}
}
行或您的循环无用。然后在列表中收集非空对象,并将其放在break;
方法中execute
。
toArray
<强>更新强>
关于 LinkedList<Object> list = new LinkedList<Object>();
for (int i=0; i<favorites.length; i++) {
if(favorites[i]!=null){
//mServiceTask.execute(new Object[]{favorites[i]}); //<--- Problem here
list.add(favorites[i]);
//break;
}
else{
//break;
}
}
mServiceTask.execute(list.toArray());
课程。在ServiceTask
中,您只使用doInBackground
- 第一个数组元素。所以应该有:
params[0]