Skip to content

Instantly share code, notes, and snippets.

@ramsane
Created January 21, 2016 06:52
Show Gist options
  • Save ramsane/cc25f319c9f13a15a6f6 to your computer and use it in GitHub Desktop.
Save ramsane/cc25f319c9f13a15a6f6 to your computer and use it in GitHub Desktop.
/*
*
* If you want you can combine both in single
* function so that you can reduce some code.
* I have done it as per my requirements.
* Never mind about Hash map and all other stuff.
* input parameter contains result string from trailers Request from api
* and passed to it.
*
*/
// Helper method to fetch trailer url from Json String
private HashMap parseJsonForTrailers(String json) throws JSONException {
JSONObject root = new JSONObject(json);
JSONArray results = root.getJSONArray("results");
String key;
String name;
if(results.length() == 0){
DetailsHolder.trailers.addView(getEmptyTextView("No reviews for this movie"));
return null;
}
HashMap movie_trailers = new HashMap(results.length());
for (int i = 0; i < results.length(); i++) {
//Fetch youtube link and attach to the parent
key = results.getJSONObject(i).getString("key");
name = results.getJSONObject(i).getString("name");
movie_trailers.put(key,name);
}
return movie_trailers;
}
/*
* R.layout.list_item_trailer is layout for single trailer.
* DetailsHolder.trailers is reference to Linear layout for trailers
* in movieDetailfragment.xml to which each trailer is added.
*
* If condition at last is just to make sure that the
* movie had trailers. So that we can update Share Action
* provider to respective movie in detail fragment.
*/
// Method to set trailers to respective views.
public void setMovieTrailers(Map trailers){
if(trailers == null || trailers.size()==0){
return;
}
// Send trailers to parcellable class
mTotal_movie_details.setTrailers(trailers);
LinearLayout trailer;
String key;
String name;
Iterator i = trailers.entrySet().iterator();
while (i.hasNext()){
Map.Entry me = (Map.Entry) i.next();
key = (String) me.getKey();
name = (String) me.getValue();
trailer = (LinearLayout) LayoutInflater.from(getActivity())
.inflate(R.layout.list_item_trailer, DetailsHolder.trailers, false);
((TextView)trailer.findViewById(R.id.list_item_trailer_number)).setText(name);
trailer.setTag(key);
trailer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + v.getTag()));
if (i.resolveActivity(getActivity().getPackageManager()) != null)
startActivity(i);
}
});
DetailsHolder.trailers.addView(trailer);
}
if(DetailsHolder.trailers.getChildCount() > 0){
String shareUrl = (String) DetailsHolder.trailers.getChildAt(0).getTag();
if(mShareActionProvider != null)
mShareActionProvider.setShareIntent(sharetrailer("http://www.youtube.com/watch?v=" + shareUrl));
}
} //End of setMovie Trailers method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment