简体   繁体   English

自定义ArrayAdapter,没有List类型参数

[英]Custom ArrayAdapter with no List type parameter

I want to create my own ArrayAdapter calling just the super constructor that has only 2 parameters, the context and the resource layout id. 我想创建自己的ArrayAdapter,只调用只有2个参数的超级构造函数,即上下文和资源布局ID。 The problem is that the getView method is never called if I use that constructor, but if I use the other constructors were I pass a List object parameter or an array, it will call my getView method. 问题是如果我使用该构造函数,则永远不会调用getView方法,但如果我使用其他构造函数,则传递List对象参数或数组,它将调用我的getView方法。 I tried to override the isEmpty method and still nothing, also this method is not called either. 我试图覆盖isEmpty方法,但仍然没有,也没有调用此方法。

Is there any way to make sure the getView method is called just using the super constructor with 2 parameters (context and resource layout id) ? 有没有办法确保只使用带有2个参数(上下文和资源布局ID)的超级构造函数调用getView方法?

Thank you. 谢谢。

Since you're not passing a List to the super class when using the two-parameter constructor, you'll need to override the getCount() method to return the size of your List. 由于在使用双参数构造函数时没有将List传递给超类,因此您需要覆盖getCount()方法以返回List的大小。 Otherwise, the AdapterView will think there are zero items, and will not attempt to call getView() . 否则,AdapterView会认为零项,并且不会尝试调用getView() For example: 例如:

@Override
public int getCount()
{
    return list.size();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM