需要有关自定义 Spinner/ArrayAdapter 设置的帮助
编辑:请忽略这个问题 - 我已经设法以一种适合我需要的方式解决它(请参阅下面我自己的答案)。
我有一个 WeatherSpinner 类,它扩展了 Spinner。该类显示了我最初使用 ArrayAdapterArrayAdapter
(Locale 是我的抽象“空”类)自己的)。
当尝试使用以下内容填充我的 ArrayAdapter 时,我收到 ClassCastException
...
protected ArrayList<?> theList;
protected ArrayAdapter<Locale> aa = null;
...
protected void updateContents(ArrayList<?> list, int selectedItem) {
theList = list;
// Exception thrown on next line
aa = new ArrayAdapter<Locale>(theContext, android.R.layout.simple_spinner_item,
(Locale[]) theList.toArray());
...
}
我将 RegionList
对象作为“list”参数传递到 updateContents() 中RegionList 扩展了 ArrayList
,并且 Region 扩展了 Locale
。我还重写了 Region 的 toString() 方法以返回有效的字符串。
我在这里没有看到什么?我对 ArrayList.toArray() 工作方式的理解有误吗?
EDIT: Please ignore this question - I've managed to solve it in a way which works fine for what I need (see my own answer below).
I have a WeatherSpinner class which extends Spinner. The class shows region names which I originally did using an ArrayAdapter<String>
but I now want to use ArrayAdapter<Locale>
(Locale is an abstract 'empty' class of my own).
I'm getting a ClassCastException
when trying to populate my ArrayAdapter with the following...
protected ArrayList<?> theList;
protected ArrayAdapter<Locale> aa = null;
...
protected void updateContents(ArrayList<?> list, int selectedItem) {
theList = list;
// Exception thrown on next line
aa = new ArrayAdapter<Locale>(theContext, android.R.layout.simple_spinner_item,
(Locale[]) theList.toArray());
...
}
I'm passing a RegionList
object into updateContents() as the 'list' parameter and RegionList extends ArrayList<Region>
, and Region extends Locale
. I've also overriden Region's toString()
method to return a valid String.
What am I not seeing here? Am I wrong about the way ArrayList<?>.toArray()
works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,解决了。
我需要使用 ArrayList 的 toArray(T[]contents) 方法,如下所示......
OK, solved.
I needed to use the toArray(T[] contents) method of ArrayList as follows...