尝试使用不兼容的返回类型

时间:2018-07-27 15:20:23

标签: java android admob

有人可以帮助我解决我不断遇到的错误吗?我正在尝试在recyclerview中的项目之间实施admob横幅广告的程序。每件事都可以,但仍然是这个错误阻止了我继续前进。

public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {   
    public static final String TAG = RecipeAdapter.class.getSimpleName();   
    public static final HashMap<String, Integer> LABEL_COLORS = new HashMap<String, Integer>()   {{
        put("Low-Carb", R.color.colorLowCarb);
        put("Low-Fat", R.color.colorLowFat);
        put("Low-Sodium", R.color.colorLowSodium);
        put("Medium-Carb", R.color.colorMediumCarb);
        put("Vegetarian", R.color.colorVegetarian);
        put("Balanced", R.color.colorBalanced); 
        }};

  private Context mContext;   
  private LayoutInflater mInflater;   
  private ArrayList<Recipe> mDataSource;   
  private static final int DEFAULT_VIEW_TYPE = 1;   
  private static final int NATIVE_AD_VIEW_TYPE = 2;

  public RecipeAdapter(Context context, ArrayList<Recipe> items) {
    mContext = context;
    mDataSource = items;
    mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   }

  @Override   public int getItemViewType(int position) {
    // Change the position of the ad displayed here. Current is after 5
    if ((position + 1) % 6 == 0) {
      return NATIVE_AD_VIEW_TYPE;
    }
    return DEFAULT_VIEW_TYPE;   }

  @NonNull


  @Override   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    switch (viewType) {
      default:
        view = layoutInflater
                .inflate(R.layout.list_item_native_ad, parent, false);
        return new ViewHolder(view);
      case NATIVE_AD_VIEW_TYPE:
        view = layoutInflater.inflate(R.layout.list_item_native_ad, parent, false);
        return new ViewHolderAdMob(view);
    }

  }

  @Override   public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    // Get relevant subviews of row view
    TextView titleTextView = holder.titleTextView;
    TextView subtitleTextView = holder.subtitleTextView;
    TextView detailTextView = holder.detailTextView;
    ImageView thumbnailImageView = holder.thumbnailImageView;


    //Get corresponding recipe for row   final   Recipe recipe = (Recipe) getItem(position);

    // Update row view's textviews to display recipe information
    titleTextView.setText(recipe.title);
    subtitleTextView.setText(recipe.description);
    detailTextView.setText(recipe.label);

    // Use Picasso to load the image. Temporarily have a placeholder in case it's slow to load
    Picasso.with(mContext).load(recipe.imageUrl).placeholder(R.mipmap
            .ic_launcher).into(thumbnailImageView);


    holder.parentView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {


    Intent detailIntent = new Intent(mContext, RecipeDetailActivity.class);
    detailIntent.putExtra("title", recipe.title);
    detailIntent.putExtra("url", recipe.instructionUrl);

    mContext.startActivity(detailIntent);

      }
    });

    // Style text views
    Typeface titleTypeFace = Typeface.createFromAsset(mContext.getAssets(),
            "fonts/JosefinSans-Bold.ttf");
    titleTextView.setTypeface(titleTypeFace);
    Typeface subtitleTypeFace = Typeface.createFromAsset(mContext.getAssets(),
            "fonts/JosefinSans-SemiBoldItalic.ttf");
    subtitleTextView.setTypeface(subtitleTypeFace);
    Typeface detailTypeFace = Typeface.createFromAsset(mContext.getAssets(),
            "fonts/Quicksand-Bold.otf");
    detailTextView.setTypeface(detailTypeFace);
    detailTextView.setTextColor(android.support.v4.content.ContextCompat.getColor(mContext, LABEL_COLORS
            .get(recipe.label)));

  }


  @Override   public int getItemCount() {
    return mDataSource.size();   }


  @Override   public long getItemId(int position) {
    return position;   }


  public Object getItem(int position) {
    return mDataSource.get(position);   }


  public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView titleTextView;
    private TextView subtitleTextView;
    private TextView detailTextView;
    private ImageView thumbnailImageView; private View parentView;

    public ViewHolder(@NonNull View view){
      super(view);
      // create a new "Holder" with subviews
      this.parentView = view;
      this.thumbnailImageView = (ImageView) view.findViewById(R.id.recipe_list_thumbnail);
      this.titleTextView = (TextView) view.findViewById(R.id.recipe_list_title);
      this.subtitleTextView = (TextView) view.findViewById(R.id.recipe_list_subtitle);
      this.detailTextView = (TextView) view.findViewById(R.id.recipe_list_detail);

      // hang onto this holder for future recyclage
      view.setTag(this);
    }

  }

  public class ViewHolderAdMob extends RecyclerView.ViewHolder {
    private final AdView mNativeAd;

    public ViewHolderAdMob(View itemView) {
      super(itemView);
      mNativeAd = itemView.findViewById(R.id.nativeAd);
      mNativeAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
          super.onAdLoaded();
          // if (mItemClickListener != null) {
          Log.i("AndroidBash", "onAdLoaded");
          // }
        }

        @Override
        public void onAdClosed() {
          super.onAdClosed();
          //   if (mItemClickListener != null) {
          Log.i("AndroidBash", "onAdClosed");
          //  }
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
          super.onAdFailedToLoad(errorCode);
          //  if (mItemClickListener != null) {
          Log.i("AndroidBash", "onAdFailedToLoad");
          //  }
        }

        @Override
        public void onAdLeftApplication() {
          super.onAdLeftApplication();
          //  if (mItemClickListener != null) {
          Log.i("AndroidBash", "onAdLeftApplication");
          //   }
        }

        @Override
        public void onAdOpened() {
          super.onAdOpened();
          //  if (mItemClickListener != null) {
          Log.i("AndroidBash", "onAdOpened");
          //  }
        }
      });
      AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice("") // Remove this before publishing app
              .build();
      mNativeAd.loadAd(adRequest);
    }

  }

}

enter image description here

1 个答案:

答案 0 :(得分:1)

返回类型必须是您为适配器类声明的ViewHolder类型。

例如,从Android RecyclerView example page

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
                                //                  ^^^^^^^^^^^^^^^^^^^^^^
                                //  It should return this type ^

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // your adapter
    }

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        // Note: returns MyAdapter.MyViewHolder, not RecyclerView.ViewHolder
    }
}

就您而言,您有

public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder>

这意味着您的onCreateViewHolder必须返回RecipeAdapter.ViewHolder而不是RecyclerView.ViewHolder

也有一个单独的问题,即在同一适配器中有两种ViewHolder类型。为此,您需要将ViewHolder所基于的RecyclerView类型更改为通用类型(RecyclerView.ViewHolder)。

请查看this question,它对此有很好的答案。