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

问题描述

我是 android studios 的新手我目前正在将我的 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,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;
        }
    }
}

感谢任何帮助或建议

解决方法

改变这个

private List<Note> noteList;

为此

private List<Object> noteList;