无法将对象列表添加到我的集合中

时间:2016-07-05 06:14:13

标签: java android listview collections android-recyclerview

在我的主要活动中,我有两个列表:

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());

在这里,我将我的FeedIcyclerAdapter中的adItems()对象列表作为最后一个参数传递,在适配器类本身中,我在构造函数中调用它:

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>)

我的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。为什么我不能简单地将它们附加到我的主列表中?我该怎么做呢?

2 个答案:

答案 0 :(得分:2)

你设置错误的数组

$(document).ready(function() {
  $("#ckbCheckAll").click(function() {
    $(".checkBoxClass").prop('checked', $(this).prop('checked'));
  });
});
$(document).ready(function() {
  $('.checkBoxClass').click(function() {
    var selected = $('.checkBoxClass').length;
    if ($('.checkBoxClass:checked:enabled').length === selected) {
      $('#ckbCheckAll').prop('checked', true);
    } else {
      $('#ckbCheckAll').prop('checked', false);
    }
  })
});
$(document).ready(function() {
  count = 5;
  $('#save').click(function() {
    $('.checkBoxClass').each(function() {
      if ($(this).is(':checked')) {
        $(this).prop('disabled', true);
      }
    })
  });
  $('#add').click(function() {
    count = count + 1;
    $('#myTable').append('<tr><td><input type="checkbox" class="checkBoxClass" /> <span>Option'+count+'</span></td></tr>')
  });
})

答案 1 :(得分:0)

在我的主要活动的onCreate方法中,我将adItems设置为Feed列表,然后将其传递给我的RecyclerAdapter构造函数:

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

在我的RecyclerAdapter类中,我将所有adItem添加到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);
}