这两个构造函数:
public ArrayAdapter (Context context,
int resource)
public ArrayAdapter (Context context,
int resource,
int textViewResourceId)
当您想要创建一个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<>());
}
例如,您可能希望在活动的onCreate()方法中为ListView创建一个ArrayAdapter,但填充ListView的数据是稍后从网络请求中获取的.在这种情况下,您可以使用第一个或第二个构造函数来创建ArrayAdapter,然后在网络请求完成后向其中添加数据.
以下是一些有用的链接:
https://developer.android.com/develop/ui/views/layout/declaring-layout#AdapterViews个
[编辑] 如果使用第一个构造函数,则文本视图资源将为0,从源代码(上面的链接)可以了解到,在这种情况下,整个资源都被视为TextView:
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {