gpt4 book ai didi

When are constructors one and two used in the ArrayAdapter?(何时在ArrayAdapter中使用构造函数1和2?)

转载 作者:bug小助手 更新时间:2023-10-25 21:19:41 24 4
gpt4 key购买 nike



ArrayAapter Class have some constructors:
enter image description here

ArrayAapter类具有一些构造函数:


When are constructors one and two used? Because we don't have data structure like input array or list at all in these constructors. If possible, explain with an example. Thank you

什么时候使用构造函数1和2?因为我们在这些构造函数中根本没有像输入数组或列表这样的数据结构。如果可能的话,用一个例子来解释。谢谢你


更多回答
优秀答案推荐

Those two constructors:

这两个构造函数:


public ArrayAdapter (Context context, 
int resource)


public ArrayAdapter (Context context,
int resource,
int textViewResourceId)

are used when you want to create an ArrayAdapter but you don't have the data at the moment of its creation. You can add data later on using methods like add(), insert(), etc. They actually internally call other constructors with empty collection as parameter:

当您想要创建一个ArrayAdapter,但在创建它的时候没有数据时,可以使用。您可以稍后使用Add()、Insert()等方法添加数据。它们实际上在内部调用其他构造函数,并将空集合作为参数:


public ArrayAdapter(@NonNull Context context, @LayoutRes int resource) {
this(context, resource, 0, new ArrayList<>());
}

public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
@IdRes int textViewResourceId) {
this(context, resource, textViewResourceId, new ArrayList<>());
}

As for example, you might want to create an ArrayAdapter for a ListView in your activity's onCreate() method, but the data to populate the ListView is fetched later from a network request. In this case, you can use the first or second constructor to create the ArrayAdapter and then add data to it once your network request is complete.

例如,您可能希望在活动的onCreate()方法中为ListView创建一个ArrayAdapter,但填充ListView的数据是稍后从网络请求中获取的。在这种情况下,您可以使用第一个或第二个构造函数来创建ArrayAdapter,然后在网络请求完成后向其中添加数据。


Some useful links:

一些有用的链接:


https://developer.android.com/develop/ui/views/layout/declaring-layout#AdapterViews

Https://developer.android.com/develop/ui/views/layout/declaring-layout#AdapterViews


https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ArrayAdapter.javae

Https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ArrayAdapter.javae


[edit]
If the first constructor is used, then text view resource will be 0, from the source code (link above) you can learn that in this case the whole resource is considered a TextView:

[编辑]如果使用第一个构造函数,则文本视图资源将为0,从源代码(上面的链接)可以了解到,在这种情况下,整个资源都被视为TextView:


    if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {


These two constructors create empty ArrayAdapter instances.

这两个构造函数创建空的ArrayAdapter实例。


ArrayAdapter has methods add() and addAll() that allow you to add elements to such an empty ArrayAdapter.

ArrayAdapter有方法add()和addAll(),允许您向这样的空ArrayAdapter添加元素。


更多回答

thank you. I made an example for myself using the second constructor and using the add() and addAll() method. But about the first constructor, we have not even introduced the TextView in the constructor, I mean that if we want to add data to the adapter later, because the TextView in the Layout is not defined in the constructor, Therefore, the adapter does not know where to put this added data in this Layout.

谢谢。我使用第二个构造函数和Add()和addAll()方法为自己制作了一个示例。但是关于第一个构造函数,我们甚至还没有在构造函数中引入TextView,我的意思是,如果我们想稍后向适配器添加数据,因为布局中的TextView不是在构造函数中定义的,因此,适配器不知道将添加的数据放在这个布局中的哪里。

@amin see my edit for a case when text view resource is not provided

@Amin有关未提供文本视图资源的情况,请参阅我的编辑

I used android prepared layouts like android.R.layout.simple_list_item_1 and I understand it. also I made a layout with only one TextView on it and try first constructor, then I used add() and insert() methods on adapter object, I got it, Thank you.

我使用了android.R.layout.Simple_List_Item_1等Android准备的布局,我理解它。我还做了一个只有一个TextView的布局,并尝试了第一个构造函数,然后我对适配器对象使用了Add()和Insert()方法,我得到了,谢谢。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com