Issue
I am trying to implement Circular ProgressBar
in one of my fragment
.
I want to attain this using AsyncTask
class along with other background process and I want that progress bar
to update on onProgressUpdate()
method too.
Here's my Fragment implementing ProgressBar:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.playlist_fragment, container, false);
// Setting up progressBar
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
progressBar.setProgress(0);
return rootView;
}
Calling AsyncTask
class from within Fragment which says cannot resolve method at setProgressBr()
. I think I am not appropriately passing the parameters.
new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this)
.setProgressBar(progressBar)
.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
Here's my AsyncTask class:
public class GetPlaylistAsyncTask extends AsyncTask<String, Integer, Pair<String, List<Video>>> {
private static final String TAG = "GetPlaylistAsyncTask";
private static final Long YOUTUBE_PLAYLIST_MAX_RESULTS = 10L;
//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.
private YouTube mYouTubeDataApi;
Context mContext;
ProgressBar bar;
private AsyncResponse theListener;
public void setProgressBar(ProgressBar bar) {
this.bar = bar;
}
public GetPlaylistAsyncTask(YouTube api, Context context, PlaylistFragment frag ) {
mYouTubeDataApi = api;
mContext = context;
theListener = (AsyncResponse)frag;
}
@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());
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Integer... values) {
if (this.bar != null) {
bar.setProgress(values[0]);
}
}
@Override
protected void onPostExecute(Pair<String, List<Video>> result) {
theListener.handleGetPlaylistResult(null,result);
}
}
Will appreciate a quick fix for the above problem. Thanks in advance.
Solution
.setProgressBar(progressBar)
method has a return type void so you cannot chain the calls like this
new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this)
.setProgressBar(progressBar)
.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
Change your setProgressBar()
method to the following and it should work
public GetPlaylistAsyncTask setProgressBar(ProgressBar bar) {
this.bar = bar;
return this;
}
OR
If you don't want to change the method signature then do the following
final GetPlaylistAsyncTask getPlaylistAsyncTask = new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this);
getPlaylistAsyncTask.setProgressBar(progressBar);
getPlaylistAsyncTask.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
Also make sure both params playlistVideos.playlistId, playlistVideos.getNextPageToken()
are Strings.
Answered By - LeoNeo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.