不兼容的类型:无法将类与原生广告进行比较

Posted

技术标签:

【中文标题】不兼容的类型:无法将类与原生广告进行比较【英文标题】:incompatible types: cant compare a class to a native ad 【发布时间】:2021-06-27 22:28:01 【问题描述】:

我是 android 工作室的新手,我目前正在将 FirestoreRecyclerOptions 更改为常规回收站视图,因为我想每 5 个帖子添加一次原生广告。我面临的当前问题是我希望它返回的方法 getItemViewType 是广告还是使用实例的常规帖子。在教程视频中,他们做了一些类似于

@Override
    public int getItemViewType(int position) 
        if (noteList.get(position) instanceof UnifiedNativeAd) 
            return TYPE_AD;
        else 
            return TYPE_REG;
        
    

但是 if 语句中的条件给了我这个错误

error: incompatible types: Note cannot be converted to UnifiedNativeAd
        if (noteList.get(position) instanceof UnifiedNativeAd) 

public class Note 
    public int timestamp;
    public List<String> replies;
    public String ownerName;
    public String ownerId;
    public String text;
    public String imageURL;
    public List<String> usersLiked;
    public String message;
    public String timePosted;
    public int likes;
    public int replyCount;


    public Note() 
    

    public Note(int timestamp, String ownerId, String text, String ownerName, String imageURL, List<String> replies, List<String>
            usersLiked, String message, String timePosted, int likes, int replyCount) 
        this.timestamp = timestamp;
        this.ownerId = ownerId;
        this.text = text;
        this.ownerName = ownerName;
        this.imageURL = imageURL;
        this.replies = replies;
        this.usersLiked = usersLiked;
        this.message = message;
        this.timePosted = timePosted;
        this.likes = likes;
        this.replyCount = replyCount;
    

    public int getTime() 
        return timestamp;
    

    public String getOwnerName() 
        return ownerName;
    

    public String getId() 
        return ownerId;
    

    public String getPost() 
        return text;
    

    public List<String> getreplies() 
        return replies;
    

    public String getImageURL() 
        return imageURL;
    

    public List<String> getUsersLiked() 
        return usersLiked;
    

    public String getMessage() 
        return message;
    

    public String getTimePosted() 
        return timePosted;
    

    public int getLikes() 
        return likes;
    

    public int getReplyCount() 
        return replyCount;
    

完全回收视图

ublic class adapterRegular extends RecyclerView.Adapter<RecyclerView.ViewHolder> 
    private final static int TYPE_AD=0;
    private final static int TYPE_REG=1;

    private Context context;
    private List<Note> noteList;

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) 


    

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        context = parent.getContext();
        if(viewType == TYPE_AD)
            View view = LayoutInflater.from(context).inflate(R.layout.reclyerads,parent,false);
            return new AdTemplateViewHolder(view);
        
        View view = LayoutInflater.from(context).inflate(R.layout.list_row,parent,false);
        return new noteHolder(view);
    

    @Override
    public int getItemCount() 
        return noteList.size();
    

    public class noteHolder extends RecyclerView.ViewHolder
        TextView ownerName;
        TextView timestamp;
        TextView text;
        ImageView imageURL;
        TextView replies;
        TextView likes;
        ImageView heart;

        public noteHolder(@NonNull View itemView) 
            super(itemView);
            ownerName = itemView.findViewById(R.id.userName);
            timestamp = itemView.findViewById(R.id.time);
            text = itemView.findViewById(R.id.post);
            imageURL = itemView.findViewById(R.id.profilePic);
            replies = itemView.findViewById(R.id.REPLY);
            likes = itemView.findViewById(R.id.likes);
            heart = itemView.findViewById(R.id.heart);
        
    

    public class AdTemplateViewHolder extends RecyclerView.ViewHolder
        TemplateView templateView;

        public AdTemplateViewHolder(@NonNull View itemView) 
            super(itemView);
            templateView = itemView.findViewById(R.id.my_template);
            NativeTemplateStyle style = new NativeTemplateStyle.Builder()
                    .withMainBackgroundColor(new ColorDrawable(Color.parseColor("#FFFFFF"))).build();
            templateView.setStyles(style);
        
        public void setUnifiedNativeAd(UnifiedNativeAd ads)
            templateView.setNativeAd(ads);
        

    

    @Override
    public int getItemViewType(int position) 
        if (noteList.get(position) instanceof UnifiedNativeAd) 
            return TYPE_AD;
        else 
            return TYPE_REG;
        
    

请提供任何帮助或建议

【问题讨论】:

【参考方案1】:

改变这个

private List<Note> noteList;

到这里

private List<Object> noteList;

【讨论】:

以上是关于不兼容的类型:无法将类与原生广告进行比较的主要内容,如果未能解决你的问题,请参考以下文章

不兼容的类型:RecViewFragment 无法转换为 Fragment

jquery不兼容低版本ie浏览器怎么解决?

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

如何解决数据类型 ntext 和 varchar 在not equal to 运算符中不兼容?

为啥会出现错误:类与键名的键值编码不兼容?

大型数据集,不同数据库之间的数据比较不兼容