ArrayList <object>中的不兼容类型错误

时间:2017-09-29 13:41:29

标签: java android arraylist casting android-recyclerview

我正在尝试将广告放在当前正在解析JSON文件的RecyclerView中,问题是当我尝试用广告或JSON中的数据填充时,ArrayList会抛出一个转换错误。

错误发生在两个地方,一个位于RecyclerViewAdapter()方法正文,另一个位于BindViewHolder()

以下是我在RecyclerViewAdapter.java中的代码(错误在第5行评论)

private ArrayList<Object> arrayList = new ArrayList<>();

public RecyclerViewAdapter(Context context, ArrayList<CardSetterGetter> arrayList, ArrayList<String> favouriteItemList, int totalCount) {
    this.context = context;
    this.arrayList = arrayList; //Incompatible types.  Required: ArrayList<java.lang.object>;    found: ArrayList<com.comp.app.modal.CardSetterGetter>
    this.favouriteItemList = favouriteItemList;
    this.totalCount = totalCount;

}

onBindViewHolder()(错误在第27行评论)

@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {

    int viewType = getItemViewType(position);

    switch (viewType){

        case AD_VIEW_TYPE:
            NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder)holder;
            NativeExpressAdView adView = (NativeExpressAdView)arrayList.get(position);

            ViewGroup adCardView = (ViewGroup)nativeExpressHolder.itemView;
            adCardView.removeAllViews();

            if(adView.getParent() != null){
                ((ViewGroup)adView.getParent()).removeView(adView);
            }

            adCardView.addView(adView);
            break;

        case MENU_ITEM_VIEW_TYPE:

        default:
            String card_image;
            CardSetterGetter cardSetterGetter;
            cardSetterGetter = arrayList.get(position); //Incompatible types.  Required: ArrayList<com.comp.app.modal.CardSetterGetter>;    found: ArrayList<java.lang.object>;

            card_image = constants.SERVERIP.concat(cardSetterGetter.getImageurl());
            holder.cardSetterGetter = cardSetterGetter;
            holder.title.setText(cardSetterGetter.getImagetitle()); //set title

            PointF focusPoint = new PointF(0.5f, 0f);
            // your app populates the focus point

            holder.image // set image
                    .getHierarchy()
                    .setActualImageFocusPoint(focusPoint);
            holder.image.setImageURI(Uri.parse(card_image));
    }

}

这是我的CardSetterGetter.java

public class CardSetterGetter implements Serializable {
    private  String  imagetitle ;
    private  String  imageurl;
    private  String  description;
    private  String  tag;
    private  boolean isFavorite;

    public String getImageId() {
        return imageId;
    }

    public void setImageId(String imageId) {
        this.imageId = imageId;
    }

    public String getFavourite() {
        return favourite;
    }

    public void setFavourite(String favourite) {
        this.favourite = favourite;
    }

    private  String  imageId ;
    private  String favourite ;



    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public  String category;

    public CardSetterGetter(){

    }

    public String getImagetitle() {
        return imagetitle;
    }

    public void setImagetitle(String imagetitle) {
        this.imagetitle = imagetitle;
    }

    public String getImageurl() {
        return imageurl;
    }

    public void setImageurl(String imageurl) {
        this.imageurl = imageurl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public CardSetterGetter(String imageurl , String imagetitle , String description , String category , String tag,String imageId ,String favourite){
        this.imageurl = imageurl ;
        this.imagetitle = imagetitle ;
        this.description = description ;
        this.tag = tag ;
        this.category = category ;
        this.isFavorite =false ;
        this.imageId = imageId ;
        this.favourite = favourite ;
    }


    public boolean isFavorite() {
        return isFavorite;
    }

    public void setFavorite(String favorite) {
        favourite = favorite;
    }
}

我在StackOverFlow上尝试了一些像这样的问题(Question),但我无法理解解决方案的基本概念,我已经将数据类型更改为Object(来自CardSetterGetter)。

还要告诉我如果解决了这个问题,并且arrayList返回了预期CardSetterGetter的adView,反之亦然,我需要将所有这些语句放在try catch块中吗?

4 个答案:

答案 0 :(得分:3)

问题:您的列表包含两种项CardSetterGetterNativeExpressAdView因此您需要使用Objects列表而不是任何特定类型,因为这两种类型都没有关系加上应用在适当的地方施放

使用此

public RecyclerViewAdapter(Context context, ArrayList<Object> arrayList, ArrayList<String> favouriteItemList, int totalCount) {

并使用cast

cardSetterGetter =(CardSetterGetter ) arrayList.get(position); 

而不是

public RecyclerViewAdapter(Context context, ArrayList<CardSetterGetter> arrayList, ArrayList<String> favouriteItemList, int totalCount) {
  

还要告诉我如果这个问题的解决问题得到解决会发生什么   arrayList返回期望CardSetterGetter的adView或   反之亦然,我是否需要将所有这些语句放在try catch块中?

getItemViewType(position)根据position

工作
   // ITEMS_PER_AD = 8
   return (position % MainActivity.ITEMS_PER_AD == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
                : MENU_ITEM_VIEW_TYPE;

您的列表中position % 8 == 0项(0,8,16,24,32 ...)的平均结果将属于NATIVE_EXPRESS_AD_VIEW_TYPE类型,因此它position依赖数学。

答案 1 :(得分:1)

问题出在你的会员定义中:

private ArrayList<Object> arrayList = new ArrayList<>();

可能应该是:

private ArrayList<CardSetterGetter> arrayList = new ArrayList<>();

此处的问题是ArrayList<CardSetterGetter>无法分配给ArrayList<Object> - 尽管CardSetterGetter(以及所有对象)可分配给Object

答案 2 :(得分:0)

像这样改变arraylist初始化

private ArrayList<CardSetterGetter> arrayList = new ArrayList<>();

答案 3 :(得分:0)

更改RecyclerViewAdapter.java中的ArrayList<Object> arrayList

private ArrayList<Object> arrayList = new ArrayList<>();

private ArrayList<CardSetterGetter> arrayList = new ArrayList<>();

这将解决您的问题