ArrayAdapter在Android中创建简单的列表视图视图、简单、列表、ArrayAdapter

2023-09-12 22:00:08 作者:十拿九吻

我试图创建一个活动在Android中,该活动只包含了的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

但我仍感到无法理解清楚!

But still I am unable to understand it clearly!

其中最大的疑问是,为什么构造函数需要一个的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 talking that this is the only one constructor, but what I try to explain is that I am unable to understand it.

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

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

Simple ListView控件使用ArrayAdapter的例子​​。

但同样我的主要的疑问是,为什么它需要一个TextView资源ID?

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

如果任何人都可以用例子来说明这将是很大的帮助。

If anybody can explain it with example it will be great 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). 

在你的情况,这将是:

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.