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, max开发者_开发知识库Results, 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;
}
精彩评论