android中的ArrayAdapter创建简单的listview [英] ArrayAdapter in android to create simple listview

查看:27
本文介绍了android中的ArrayAdapter创建简单的listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Android 中创建一个 Activity,这个 Activity 只包含一个 ListView 没有别的.

I tried to create an Activity in Android, This Activity only contains a ListView nothing else.

据我所知,要填充列表视图,我们需要使用 ArrayAdapter.

As I know to fill the listview we need to use an ArrayAdapter.

为了理解 ArrayAdapter,我阅读了以下链接:

So to understand the ArrayAdapter I have read the following link:

http://developer.android.com/reference/android/widget/ArrayAdapter.html

可是还是看不懂!

最大的疑问之一是为什么构造函数需要 TextView 资源 id 而我的活动没有任何我应该提供的 TextViews?

One of the biggest doubt is why the constructor needs a TextView resource id while my activity is not having any TextViews what I should have to give it?

我并不是说这是唯一的构造函数,只是我无法理解其背后的逻辑.

I am not saying that this is the only constructor, just that I'm unable to understand the logic behind it.

为了创建一个简单的列表视图,我还参考了以下链接:

In order to create a simple listview I also referred to the following link:

使用 ArrayAdapter 示例的简单 ListView.

但我的主要疑问是为什么它需要 TextView 资源 ID?

But again my main doubt is why it does it need a TextView resource id?

如果有人能用一个例子来解释它会很有帮助.

If anybody can explain it with an example it will be very helpful.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, values);

推荐答案

ArrayAdapter 使用 TextView 来显示其中的每个项目.在幕后,它使用它持有的每个对象的 toString() 方法并将其显示在 TextView 中.ArrayAdapter 有许多可以使用的构造函数以及您在示例中使用的构造函数是:

ArrayAdapter uses a TextView to display each item within it. Behind the scenes, it uses the toString() method of each object that it holds and displays this within the TextView. ArrayAdapter has a number of constructors that can be used and the one that you have used in your example is:

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

默认情况下,ArrayAdapter 使用默认的 TextView 来显示每个项目.但是,如果您愿意,您可以创建自己的 TextView 并通过扩展 TextView 类来实现您想要的任何复杂设计.这将不得不进入布局供您使用.您可以在 textViewResourceId 字段中引用它以将对象绑定到此视图而不是默认视图.

By default, ArrayAdapter uses the default TextView to display each item. But if you want, you could create your own TextView and implement any complex design you'd like by extending the TextView class. This would then have to go into the layout for your use. You could reference this in the textViewResourceId field to bind the objects to this view instead of the default.

为了您的使用,我建议您使用构造函数:

For your use, I would suggest that you use the constructor:

ArrayAdapter(Context context, int resource, T[] objects). 

在您的情况下,这将是:

In your case, this would be:

ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values)

应该没问题.这会将每个字符串绑定到默认的 TextView 显示 - 纯白色背景.

and it should be fine. This will bind each string to the default TextView display - plain and simple white background.

所以要回答您的问题,您不必使用 textViewResourceId.

So to answer your question, you do not have to use the textViewResourceId.

这篇关于android中的ArrayAdapter创建简单的listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆