I am trying to create a listview with youtube videos from json and I couldn't manage to implement the youtube player fragment into my VideoAdapter.
I have checked the sample codes from youtube api but I am not that advance yet. How can I implement youtube player fragment into my VideoAdapter file.
VideoAdapter.java
public class VideoAdapter extends ArrayAdapter<VideosActivity.Videos>{
ArrayList<VideosActivity.Videos> videoList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public VideoAdapter(Context context, int resource, ArrayList<VideosActivity.Videos> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
videoList = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.thumb.setImageResource(R.drawable.noimage);
new DownloadImageTask(holder.thumb).execute(videoList.get(position).getThumb());
return v;
}
static class ViewHolder {
public ImageView thumb;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
After a lot of research I achieved what I need. Anyone who needs the list videos from a playlist the code is above.