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

发布于 2024-12-08 21:19:22 字数 679 浏览 1 评论 0原文

我将如何将此代码插入到 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() 吗?如果是的话,它看起来会怎么样?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-12-15 21:19:22

是的,我将创建一个自定义适配器并使用 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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文