如何制作一个每行都有一个ImageView和TextView的android ListView?

发布于 2024-12-07 17:57:19 字数 394 浏览 0 评论 0 原文

可能的重复:
我可以有一个Android 上带有文本视图的列表视图和图像图标

如何在每行中制作一个带有 ImageView 和 TextView 的 android ListView?

我正在开发一个 Android 应用程序,该应用程序将有一个带有 ListView 的屏幕,并且每行都需要一个 ImageView 和一个 TextView...有人可以帮我提供一些线索和示例吗

Possible Duplicate:
Can I have a List view and images icon with textview on the Android

How to make a android ListView with a ImageView and TextView in Each Line?

I am working on a Android app that is going to have a screen with a ListView on it and its going to need a ImageView and a TextView on each line... can someone please help me out with some clues and samples

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

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

发布评论

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

评论(3

七婞 2024-12-14 17:57:19

您需要教您的 ListAdapter 如何执行此操作。如果这是一个 ArrayAdapter,则可以通过对其进行子类化并重写 getView() 来实现。您可以设计自己的自定义行并使用它们,在加载行时倒入 ImageViewTextView 的数据。

这是来自 rel="nofollow">我的一本书经历了这个过程。 这是该章中介绍的示例项目的源代码

You will need to teach your ListAdapter how to do that. This may be by subclassing it and overriding getView(), if this is an ArrayAdapter. You can design your own custom rows and use those, pouring in the data for the ImageView and TextView as the rows get loaded.

Here is a free excerpt from one of my books that goes through the process. Here is the source code to the sample projects profiled in that chapter.

素衣风尘叹 2024-12-14 17:57:19

如前所述,您需要扩展适配器并在 ListActivity 中使用它。使用 TextView 和 ImageView 创建一个 XML 文件(使用 LinearLayout 或任何其他布局)。您可以向 TextView 和 ImageView 添加 ID,以便根据列表中的位置更改它们。

以下代码示例创建三行并将其中的文本设置为 a、b、c。

public class MyActivity extends ListActivity {

    private String[] data = {"a","b","c"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new MyAdapter(this, R.layout.rowxml, data));
    }

    private class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context c, int i, String[] s) {
            super(c, i, s);
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.rowxml, null);
            }

            TextView tw = (TextView) v.findViewById(R.id.text);
            if (tw != null) tw.setText(data[position]);

            // You can do something similar with the ImageView  

            return v;
        }
    }
}

As mentioned, you will need to extend an adapter and use it in your ListActivity. Make an XML file with a TextView and an ImageView (using LinearLayout or any other layout). You can add IDs to TextView and ImageView so you can change them according to the position in the list.

Here is a code example that creates three rows and sets the text in them to a, b, c.

public class MyActivity extends ListActivity {

    private String[] data = {"a","b","c"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new MyAdapter(this, R.layout.rowxml, data));
    }

    private class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context c, int i, String[] s) {
            super(c, i, s);
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.rowxml, null);
            }

            TextView tw = (TextView) v.findViewById(R.id.text);
            if (tw != null) tw.setText(data[position]);

            // You can do something similar with the ImageView  

            return v;
        }
    }
}
清风夜微凉 2024-12-14 17:57:19

简而言之,您应该使用 ListActivity。您将能够为每行设置布局 xml。

看看这个
教程:

您需要做的事情是相同的,但是对其进行调整以具有除了文本视图之外还有图像视图。

In short, you should use a ListActivity. You'll be able to set the layout xml for each row.

Take a look at this
tutorial:

What you need to do is the same, but adapt it to have an ImageView in addition to a textview.

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