如何将字符串返回到自定义ListView?

时间:2011-10-05 20:08:13

标签: android android-listview

如何将此代码插入到android中的ListView中?

  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("************************************");         }
        }

}

我会创建自定义适配器并使用getView()吗?如果是这样,它看起来怎么样?

1 个答案:

答案 0 :(得分:1)

是的,我会创建一个自定义适配器并使用getView()。以下是使用TextView和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;
    }