app中有两个Arraylist
。第一个Arraylist is ArrayList<Thumbnail> videoList = new ArrayList<>()
和第二个ArrayList是ArrayList<Giphy> giphylist = new ArrayList()
。
giasterist arraylist项目在videoList中更新。如何更新第一个ArrayList videoList项目值中的第二个ArrayList GiphyList项目值?
我尝试了addAll()函数。可以使用此函数覆盖Arraylist吗?
就像这样:videoList = new ArrayList<Thumbnail>(giphyList);
这也给错误:无法解析构造函数ArrayList(java.util.List)
Thumbnail.java
public class Thumbnail {
private String gif,videoUrl;
private int id, thumbUp, thumbDown;
public Thumbnail (int id,String gif,String videoUrl,int thumbUp,int thumbDown){
this.gif = gif;
this.videoUrl = videoUrl;
this.thumbUp =thumbUp;
this.thumbDown = thumbDown;
this.id = id;
}
public String getGif() {
return gif;
}
public void setGif(String gif) {
this.gif = gif;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
public int getId() {
return id;
}
public int getThumbUp() {
return thumbUp;
}
public int getThumbDown() {
return thumbDown;
}
public void setId(int id) {
this.id = id;
}
public void setThumbUp(int thumbUp) {
this.thumbUp = thumbUp;
}
public void setThumbDown(int thumbDown) {
this.thumbDown = thumbDown;
}
}
Giphy.java
@Entity
public class Giphy {
@Id(assignable = true)
private long id;
private String url;
private int thumbUp;
private int thumbDown;
private String gif;
// public Giphy(){
// }
public Giphy(long id,String gif,String url,int thumbUp,int thumbDown){
this.id = id;
this.url = url;
this.thumbUp = thumbUp;
this.thumbDown = thumbDown;
this.gif = gif;
}
public long getId() {
return id;
}
public String getUrl() {
return url;
}
public int getThumbUp() {
return thumbUp;
}
public int getThumbDown() {
return thumbDown;
}
public void setId(long id) {
this.id = id;
}
public void setUrl(String url) {
this.url = url;
}
public void setThumbUp(int thumbUp) {
this.thumbUp = thumbUp;
}
public void setThumbDown(int thumbDown) {
this.thumbDown = thumbDown;
}
public String getGif() {
return gif;
}
public void setGif(String gif) {
this.gif = gif;
}
}
答案 0 :(得分:0)
Thumbnail
和Giphy
是不兼容的类型。创建映射功能/构造函数以从一种类型切换到另一种类型,或者将它们组合成一种类型。
例如:
class Giphy {
//...
Thumbnail toThumbnail() {
return new Thumbnail(id,gif,videoUrl,thumbUp,thumbDown);
}
}
而不是videoList = new ArrayList<>(giphyList);
final List<Thumbnail> thumbnails = giphyList.stream().map(Giphy::toThumbnail).collect(toList());
答案 1 :(得分:0)
最好的解决方案是对象。
ArrayList<Object> allItems = new ArrayList<>();
现在将缩略图Arraylist添加到allItems。
allItems.add(thumbnailList);
最后结合技师。
allItems.addAll(giphylist);
从Object ArrayList获取价值
Thumbnail thumbnail =(Thumbnail)allItems.get(position index);
System.out.println(thumbnail.videoUrl);