我正在尝试在AdMob
中实施relativelayout
原生广告,但是当我执行以下行时,我会收到错误。
private void addNativeExpressAds() {
for (int i = 0; i < mRecyclerViewItems.size(); i += 8) {
NativeExpressAdView adView = new NativeExpressAdView(getActivity());
adView.setAdUnitId("");
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
}
}
我的Adapter
public class RecGridAdapter extends RecyclerView.Adapter<RecGridAdapter.ViewHolder> implements Filterable {
private final int mBackground;
private List<Recipe> original_items = new ArrayList<>();
private List<Recipe> filtered_items = new ArrayList<>();
private ItemFilter mFilter = new ItemFilter();
private final TypedValue mTypedValue = new TypedValue();
private static Context ctx;
private ImageLoader imgloader = ImageLoader.getInstance();
private static final int ITEM = 0;
private static final int AD_VIEW_TYPE = 1;
int[] viewTypes;
private List<Object> mRecyclerViewItems = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView name;
public TextView bellow_name;
public TextView duration1;
public ImageView image;
public CardView lyt_parent;
private Typeface tf;
private TextView news_date;
public ViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.name);
tf = Typeface.createFromAsset(v.getContext().getAssets(), "Roboto-Bold.ttf");
name.setTypeface(tf);
bellow_name = (TextView) v.findViewById(R.id.bellow_name);
tf = Typeface.createFromAsset(v.getContext().getAssets(), "Roboto.ttf");
bellow_name.setTypeface(tf);
news_date= (TextView) v.findViewById(R.id.news_date);
tf = Typeface.createFromAsset(v.getContext().getAssets(), "Roboto.ttf");
news_date.setTypeface(tf);
image = (ImageView) v.findViewById(R.id.image);
lyt_parent = (CardView) v.findViewById(R.id.lyt_parent);
}
}
public class NativeExpressAdViewHolder extends RecGridAdapter.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
public Filter getFilter() {
return mFilter;
}
// Provide a suitable constructor (depends on the kind of dataset)
public RecGridAdapter(Context ctx, List<Recipe> items) {
this.ctx = ctx;
original_items = items;
filtered_items = items;
ctx.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
}
@Override
public RecGridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.native_ad, parent, false);
ViewHolder vh1 = new ViewHolder(nativeExpressLayoutView);
return vh1;
case ITEM:
default:
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recipe, parent, false);
v.setBackgroundResource(mBackground);
ViewHolder vh = new ViewHolder(v);
return vh;
}
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case AD_VIEW_TYPE:
NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder)viewHolder;
NativeExpressAdView adView = (NativeExpressAdView)mRecyclerViewItems.get(position);
ViewGroup adCardView = (ViewGroup)nativeExpressHolder.itemView;
adCardView.removeAllViews();
if (adView.getParent() != null) {
((ViewGroup)adView.getParent()).removeView(adView);
}
adCardView.addView(adView);
break;
case ITEM:
default:
final Recipe p = filtered_items.get(position);
viewHolder.name.setText(p.name);
viewHolder.bellow_name.setText(p.bellow_name);
viewHolder.news_date.setText(p.news_date);
imgloader.displayImage(Constant.getURLimgRecipe(p.image), viewHolder.image);
viewHolder.lyt_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(ctx, ActivityRecipeDetails.class);
i.putExtra(ActivityRecipeDetails.EXTRA_OBJCT, p);
ctx.startActivity(i);
}
});
//populate the RecyclerView
//holder.title.setText(list.get(position).getTitle());
//holder.description.setText(list.get(position).getDescription());
}
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return filtered_items.size();
}
@Override
public long getItemId(int position) {
return (position % 8== 0) ? AD_VIEW_TYPE: ITEM;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String query = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<Recipe> list = original_items;
final List<Recipe> result_list = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++) {
String str_title = list.get(i).name;
String str_cat = list.get(i).category_name;
if (str_title.toLowerCase().contains(query) || str_cat.toLowerCase().contains(query)) {
result_list.add(list.get(i));
}
}
results.values = result_list;
results.count = result_list.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered_items = (List<Recipe>) results.values;
notifyDataSetChanged();
}
}
}
RecFragment.java
public class RecFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_recipes, null);
// activate fragment menu
setHasOptionsMenu(true);
addNativeExpressAds ();
}
private void addNativeExpressAds() {
for (int i = 0; i < mRecyclerViewItems.size(); i += 8) {
NativeExpressAdView adView = new NativeExpressAdView(getActivity());
adView.setAdUnitId("");
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
}
}
}
知道什么是错的吗?