Java 试图使用不兼容的返回类型
有人能帮我解决这个我一直犯的错误吗?我试图在recyclerview中的项目之间实现admob横幅广告的程序。一切都很好,但仍然有一个错误阻止了我继续下去Java 试图使用不兼容的返回类型,java,android,admob,Java,Android,Admob,有人能帮我解决这个我一直犯的错误吗?我试图在recyclerview中的项目之间实现admob横幅广告的程序。一切都很好,但仍然有一个错误阻止了我继续下去 public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> { public static final String TAG = RecipeAdapter.class.getSimpleName();
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);
}
}
}
公共类RecipeAdapter扩展了RecyclerView.Adapter{
公共静态最终字符串标记=RecipeAdapter.class.getSimpleName();
public static final HashMap LABEL_COLORS=new HashMap(){{
put(“低碳水化合物”,R.color.colorLowCarb);
放(“低脂”,R.颜色。低脂肪);
put(“低钠”,R.color.colorLowNa);
put(“中等碳水化合物”,R.color.colorMediumCarb);
put(“素食者”,R.color.COLORREENT);
放置(“平衡”,R.颜色。颜色平衡);
}};
私有上下文;
私人停车场;
私有ArrayList mDataSource;
私有静态final int DEFAULT\u VIEW\u TYPE=1;
私有静态final int NATIVE_AD_VIEW_TYPE=2;
公共RecipeDapter(上下文、ArrayList项){
mContext=上下文;
mDataSource=项目;
mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);}
@替代公共int getItemViewType(int position){
//更改此处显示广告的位置。当前位置在5之后
如果((位置+1)%6==0){
返回本机\广告\视图\类型;
}
返回默认的\u视图\u类型;}
@非空
@在CreateViewHolder上覆盖public RecyclerView.ViewHolder(ViewGroup父级,int-viewType){
视图;
LayoutInflater LayoutInflater=LayoutInflater.from(mContext);
开关(视图类型){
违约:
视图=平面布局器
.充气(R.layout.list\u item\u native\u ad,父项,false);
返回新的ViewHolder(视图);
案例本机\广告\视图\类型:
视图=布局更平坦。充气(R.layout.list\u item\u native\u ad,parent,false);
返回新的ViewHolderAdMob(视图);
}
}
@覆盖BindViewHolder上的公共无效(@NonNull ViewHolder,int位置){
//获取行视图的相关子视图
text视图titleTextView=holder.titleTextView;
TextView subtitleTextView=holder.subtitleTextView;
TextView detailTextView=holder.detailTextView;
ImageView thumbnailImageView=holder.thumbnailImageView;
//获取行最终配方的对应配方=(配方)getItem(位置);
//更新行视图的文本视图以显示配方信息
titleTextView.setText(recipe.title);
subtitleTextView.setText(recipe.description);
detailTextView.setText(recipe.label);
//使用毕加索加载图像。临时设置一个占位符,以防加载缓慢
毕加索.with(mContext).load(recipe.imageUrl).placeholder(R.mipmap
.ic_launcher).插入(thumbnailImageView);
holder.parentView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent detailIntent=新意图(mContext,RecipeDetailActivity.class);
detailIntent.putExtra(“标题”,recipe.title);
detailIntent.putExtra(“url”,recipe.instructionUrl);
mContext.startActivity(detailIntent);
}
});
//样式文本视图
Typeface titleTypeFace=Typeface.createFromAsset(mContext.getAssets(),
“字体/JosefinSans Bold.ttf”);
setTypeface(titleTypeFace);
Typeface subtitleTypeFace=Typeface.createFromAsset(mContext.getAssets(),
“字体/JosefinSans半黑体斜体.ttf”);
subtitleTextView.setTypeface(subtitleTypeFace);
Typeface detailTypeFace=Typeface.createFromAsset(mContext.getAssets(),
“字体/Quicksand Bold.otf”);
detailTextView.setTypeface(detailTypeFace);
detailTextView.setTextColor(android.support.v4.content.ContextCompat.getColor)(mContext,LABEL\u COLORS
.get(recipe.label));
}
@重写公共int getItemCount(){
返回mDataSource.size();}
@覆盖公共长getItemId(int位置){
返回位置;}
公共对象getItem(int位置){
返回mDataSource.get(位置);}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
私有文本视图titleTextView;
私有文本视图字幕文本视图;
私有文本视图detailTextView;
private ImageView缩略图ImageView;private View父视图;
公共视图持有者(@NonNull视图){
超级(视图);
//使用子视图创建新的“支架”
this.parentView=视图;
this.thumbnailImageView=(ImageView)view.findViewById(R.id.recipe\u list\u缩略图);
this.titleTextView=(TextView)view.findViewById(R.id.recipe\u list\u title);
this.subtitleTextView=(TextView)view.findViewById(R.id.recipe\u list\u subtitle);
this.detailTextView=(TextView)view.findViewById(R.id.recipe\u list\u detail);
//抓住这个支架,以便将来回收利用
view.setTag(this);
}
}
公共类ViewHolderAdMob扩展了RecyclerView.ViewHolder{
私人最终咨询;
公共ViewHolderAdMob(查看项目视图){
超级(项目视图);
mNativeAd=itemView.findviewbyd(R.id.nativeAd);
mNativeAd.setAdListener(新的AdListener(){
@凌驾
已加载的公共无效(){
super.onadload();
//if(mItemClickListener!=null){
Log.i(“AndroidBash”、“onadload”);
// }
}
@凌驾
已关闭的()上的公共无效{
super.onAdClosed();
//if(mItemClickListener!=null){
Log.i(“AndroidBash”、“onAdClosed”);
// }
}
@凌驾
在失败的TOLOAD上公开无效(int错误代码){
super.onAdFailedToLoad(错误代码);
//if(mItemClickListener!=null){
Log.i(“AndroidBash”、“OnadFailedtolad”);
// }
}
@凌驾
onAdLeftApplication()上的公共无效{
super.onAdLeftApplication();
//if(mItemClickListener!=null){
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>