display video from database into gridview - android

I would like to display video from database into a grid view i managed to do it with images but have no idea how to do it with a videoview , i would like to do the same but for videos
public class Photoadapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<Photo> photoList;
#Override
public int getCount() {
return photoList.size();
}
#Override
public Object getItem(int position) {
return photoList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
ImageView imageView;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imageViewList);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Photo photo = photoList.get(position);
final byte[] photoImage = photo.getImage();
final Bitmap bitmap = BitmapFactory.decodeByteArray(photoImage, 0, photoImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
public Photoadapter(Context context, int layout, ArrayList<Photo> photoList) {
this.context = context;
this.layout = layout;
this.photoList = photoList;
}
}

Related

GridView swaps items

Hello my friends I have a problem. When I scroll down in my GridView and scroll up the order of the items changes. I want all itmes to stay on the same place. Below you can find my code. It would be nice if someone can help me out as this is a very irritating bug.
public class GridAdapter extends BaseAdapter {
Context context;
private final String [] data;
View view;
LayoutInflater layoutInflater;
int[] i = {
R.drawable.blue,
R.drawable.green,
R.drawable.yellow,
};
public GridAdapter(Context context, String[] data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = new View(context);
view = layoutInflater.inflate(R.layout.single_item,null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.textView);
Random rand = new Random();
int n = rand.nextInt(i.length);
imageView.setImageResource(i[n]);
textView.setText(data[position]);
}else{
view = (View)convertView;
}
return view;
} }
to faster the performance use ViewHolder like below
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.single_item, null);
final ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
final TextView textView = (TextView)convertView.findViewById(R.id.textView);
final ViewHolder viewHolder = new ViewHolder(imageView, textView);
convertView.setTag(viewHolder);
}
Random rand = new Random();
int n = rand.nextInt(i.length);
final ViewHolder viewHolder = (ViewHolder)convertView.getTag();
viewHolder.imageView.setImageResource(i[n]);
viewHolder.textView.setText(data[position]);
return convertView;
}
private class ViewHolder {
private final TextView imageView;
private final TextView textView;
public ViewHolder(TextView imageView, TextView textView) {
this.imageView = imageView;
this.textView = textView;
}
}
It worked for me after adding those 2 lines:
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}

ViewHolder doesn't work

public class ListViewAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
public ListViewAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
list = new View(context);
list = inflater.inflate(R.layout.listview_layout, null);
TextView name = (TextView) list.findViewById(R.id.location_name);
TextView desc = (TextView) list.findViewById(R.id.location_desc);
ImageView img = (ImageView) list.findViewById(R.id.location_image);
name.setText(locationDetails[i].getLocationName());
desc.setText(locationDetails[i].getLocationDesc());
img.setImageResource(locationDetails[i].getLocationIcon());
} else {
list = (View) view;
}
return list;
}
}
This code is the code before I use viewholder, it works fine. Then I tried to modify it with the use of viewholder, and compile it with gradle, all the image and textviews disappeared, I don't know what's wrong with the code below. Why can't I get any thing shown on the screen?
public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;
if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);
vh = new ViewHolder();
vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);
list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}
return list;
}
static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}
public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
Object model;
public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;
if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);
vh = new ViewHolder();
vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);
list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}
model = getItem(i);
vh.name.setText(model.getLocationName());
vh.desc.setText(model.getLocationDesc());
// same as image...
return list;
}
static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}

GridView will disappear upon scrolling

When I scroll my GridView up and down, the gridview will start lose its children and eventually (intermittently) the whole gridview will dissapear on bottom of page if I done too many scroll.
Here is my adapter code:
public class Adapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<Item> list;
public Adapter(Context _context, List<Item> _list){
this.context = _context;
this.list = _list;
this.inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return this.list.size() - 1;
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public long getItemId(int arg0) {
return 0;
}
public class ViewHolder {
ImageView imageView;
TextView itemLabel;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, .., null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(..);
holder.itemLabel = (TextView)convertView.findViewById(..);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Item item = list.get(position + 1);
holder.itemLabel.setText(item.getTitle());
holder.imageView.setImageDrawable(item.getImage());
return convertView;
}
}
What could be wrong, as my adapter is a standard adapter?

GridView Items changes and finally dissapear on Scrolling

This is my BaseAdapater class in which I am using ViewHolder.
public class CategoryAdapter extends BaseAdapter {
private Context mContext;
public int[] mThumbIds = {R.drawable.shoes,R.drawable.dress,R.drawable.purse,
R.drawable.shoes,R.drawable.dress,R.drawable.purse ,
R.drawable.shoes,R.drawable.dress,R.drawable.purse };
public WishbookCategoryAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.wishbook_gridview_items, null);
holder.Category = (TextView)convertView.findViewById(R.id.wishbook_grid_category);
holder.items = (TextView)convertView.findViewById(R.id.wishbook_grid_category_number);
holder.iv = (ImageView)convertView.findViewById(R.id.wishbook_category_image);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.iv.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));
holder.iv.setScaleType(ScaleType.FIT_XY);
holder.Category.setText("Category");
holder.items.setText("items");
return convertView;
}
private static class ViewHolder {
protected ImageView iv;
protected TextView Category;
protected TextView items;
}
}

How to return strings to a custom ListView?

How would I go about inserting this code into a ListView in android?
List<YouTubeVideo> videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout);
for (YouTubeVideo youtubeVideo : videos) {
System.out.println(youtubeVideo.getWebPlayerUrl());
System.out.println("Thumbnails");
for (String thumbnail : youtubeVideo.getThumbnails()) {
System.out.println("\t" + thumbnail);
}
System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl());
System.out.println("************************************"); }
}
}
Would I create a custom adapter and use getView()? If so how would it look?
Yes, I would create a custom adapter and use getView(). Here is some sample code for a row layout with a TextView and an ImageView:
private static class MyListAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Item> items;
private LayoutInflater mInflater;
public MyListAdapter(Context context, ArrayList<Item> items)
{
this.context = context;
this.items = items;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, parent, false);
}
Item item = items.get(position);
TextView tv = (TextView) convertView.findViewById(R.id.data_item);
tv.setText(item.getItem());
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1);
iv.setImageBitmap(item.getBitmap());
return convertView;
}

Categories

Resources