简体   繁体   English

转换ArrayList <SomeClass> 到ArrayList <SomeInterface>

[英]Casting ArrayList<SomeClass> to ArrayList<SomeInterface>

I have 3 classes representing 3 differents things: Article , Photos , and Videos 我有3个类代表3个不同的东西: ArticlePhotosVideos

All 3 things have 2 properties in common: A title and and image . 所有3件事共有2个属性: titleimage

I have an interface called ImageTitleListItem that the 3 classes implement. 我有一个名为ImageTitleListIteminterface ,3个类实现。 That interface is just a getter/setter for title and image interface只是titleimage的getter / setter

I have 3 classes that are ArrayList : 我有3个类是ArrayList

Articles extends ArrayList<Article>
Photos extends ArrayList<Photo>
Videos extends ArrayList<Video>

I have an ArrayListAdapter that display a list of those images with there title . 我有一个ArrayListAdapter ,显示那些带有title images列表。

The constructor of the ArrayListAdapter receive ArrayList<ImageTitleListItem> as a parameter ArrayListAdapter的构造函数接收ArrayList<ImageTitleListItem>作为参数

When I call it : 我打电话的时候:

adapter = new ArrayListAdapter(Articles)

I have an error: 我有一个错误:

The constructor ArrayListAdapter(Articles) is undefined

That's normal, so i tried to cast it: 这是正常的,所以我试图施展它:

adapter = new ArrayListAdapter((ArrayList<ImageTitleListItem>)Articles)

Now the error is: 现在的错误是:

Cannot cast from Articles to ArrayList<ImageTitleListItem>

I'm relativly new to java so there's a trick I'm missing, can somebody help me on this. 我是java的新手,所以有一个我不知道的技巧,有人可以帮助我。

Thanks 谢谢

需要将ArrayListAdapter构造函数定义为

 ArrayListAdapter(List<? extends ImageTitleListItem> list)

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

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