2016-07-05 60 views
-1

添加對象的名單在我的主要活動,我有兩個列表:不能到我的收藏

private List<Feed> feedItems; 

private ArrayList<Object> adItems() { 
    ArrayList<Object> adItems = new ArrayList<>(); 
    adItems.add("image"); 
    adItems.add("image"); 
    adItems.add("image"); 
    return adItems; 
} 

第一個列表代表我feedItems,這是從我的網絡模型中得到的。第二個列表只是一個靜態圖像列表。我正在嘗試設置原生廣告,但我只是後退一步,只是提供圖像的Feed以證明覆蓋我的RecyclerAdapter以生成多個視圖。

feedRecyclerAdapter = new FeedRecyclerAdapter(this, feedItems, new ImageLoader(new FeedItemFileCache(this)), adItems()); 

在這裏,我通過我的adItems()在我的FeedRecyclerAdapter作爲最後paramater對象列表,並在適配器類本身我把它在構造函數中:

public FeedRecyclerAdapter(Context context, List<Feed> feedItems, ImageLoader feedItemImageLoader, List<Object> adItems) { 
    this.context = context; 
    this.feedItems = feedItems; 
    this.feedItemImageLoader = feedItemImageLoader; 
    feedItems.addAll(adItems); 
} 

最後一行feedItems.addAll(adItems);在這裏我得到了以下錯誤消息:

Error:(43, 18) error: no suitable method found for addAll(List<Object>) 
method Collection.addAll(Collection<? extends Feed>) is not applicable 
(argument mismatch; List<Object> cannot be converted to Collection<? extends Feed>) 
method List.addAll(Collection<? extends Feed>) is not applicable 
(argument mismatch; List<Object> cannot be converted to Collection<? extends Feed>) 

我的飼料對象是這樣的:

public class Feed { 
    public static final String CLASS_NAME = "Feed"; 
    public static final String MARKET_CLASS_NAME = "MarketFeed"; 

    private String objectId; 
    private String createdAt; 
    /** 
    * Can be a ParsePointer but we are using a relational include query to get the whole object 
    */ 
    private Design designId; 

    // Not part of the model, calculated at run time. 
    private String timestampText; 
    private boolean isInPersonalGallery; 
    private boolean isLiked; 
    private List<Comment> comments; 

    // Related to Market Feed Items 
    private DisplayedSide displayedSide = DisplayedSide.FRONT; 

    public enum DisplayedSide { 
     FRONT, 
     BACK 
    } 

    public String getObjectId() { 
     return objectId; 
    } 

    public Design getDesign() { 
     return designId; 
    } 

    public Date getCreatedAt() { 
     DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()); 
     Date date = null; 
     try { 
      date = dateFormat.parse(createdAt); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
      Log.d("Feed", "Couldn't parse createdAt date when requested " + createdAt); 
     } 

     return date; 
    } 

    public String getTimestampText() { 
     return timestampText; 
    } 

    public void setTimestampText(String timestampText) { 
     this.timestampText = timestampText; 
    } 

    public boolean isInPersonalGallery() { 
     return isInPersonalGallery; 
    } 

    public void setInPersonalGallery(boolean inPersonalGallery) { 
     isInPersonalGallery = inPersonalGallery; 
    } 

    public boolean isLiked() { 
     return isLiked; 
    } 

    public void setLiked(boolean liked) { 
     isLiked = liked; 
    } 

    public List<Comment> getComments() { 
     return comments; 
    } 

    public void setComments(List<Comment> comments) { 
     this.comments = comments; 
    } 

    public DisplayedSide getDisplayedSide() { 
     return displayedSide; 
    } 

    public void setDisplayedSide(DisplayedSide displayedSide) { 
     this.displayedSide = displayedSide; 
    } 
} 

總之,我試圖在我的Collection中的主對象feedItems之間撒上一些圖像或adItems。爲什麼我不能簡單地將它們附加到我的主要列表中?我能做些什麼呢?

+3

'列表'只能保存'Feed'對象。如果你想要它,它必須是'List '。 – shmosel

+0

@bluemunch你的類型不匹配 – Nisarg

+0

你的錯誤本身描述的問題:**參數不匹配;列表無法轉換爲Collection <?擴展飼料> ** –

回答

0

設置在我設置adItems成爲飼料列表我的主要活動的onCreate方法,然後將它傳遞給我的RecyclerAdapter構造:

List<Feed> adItems = new ArrayList<>(); 
feedRecyclerAdapter = new FeedRecyclerAdapter(this, feedItems, new ImageLoader(new FeedItemFileCache(this)), adItems); 

在我的RecyclerAdapter類將所有adItems添加到feedItems列表,以便我可以在單個列表上操作:

public FeedRecyclerAdapter(Context context, List<Feed> feedItems, ImageLoader feedItemImageLoader, List<Feed> adItems) { 
    this.context = context; 
    this.feedItems = feedItems; 
    this.feedItemImageLoader = feedItemImageLoader; 
    feedItems.addAll(adItems); 
} 
2

你在錯誤的陣列

feedItems.addAll(adItems);//you are set adItems in wrong arraylist (feedItems is a feetItem array list not a adItem list) 

public FeedRecyclerAdapter(Context context, List<Feed> feedItems, ImageLoader feedItemImageLoader, List<Object> adItems) { 
this.context = context; 
this.feedItems = feedItems; 
this.feedItemImageLoader = feedItemImageLoader; 
feedItems.addAll(adItems); 
}` 
+0

我想將「addItems」廣告給feedItems列表。這是我的意圖。 – santafebound