我想检索索引号为3的广告。但是我的代码是在由Firebase检索的每个对象之间检索广告。有什么可以告诉我我的错误吗?
---这是我的RecyclerView ---
private static final int ads = 0, content = 1, book = 2;
private Context mContext;
private List<Object> mDataset;
// View Holder for data item
public class DataViewHolder extends RecyclerView.ViewHolder {
TextView views_discover, titleofthecontent;
Context ctx;
Button gotoFeed_discover;
ImageView img_discover;
View postPosted;
DataViewHolder(View view) {
super(view);
this.ctx = ctx;
postPosted = itemView;
views_discover = view.findViewById(R.id.views_discover);
gotoFeed_discover = view.findViewById(R.id.gotofeed_discover);
titleofthecontent = view.findViewById(R.id.title_discover);
img_discover = view.findViewById(R.id.imageView_discover);
}
public void setDateInDiscover( final Context ctx, final Feed_modell modell){
String Name = modell.getName2();
String Title = modell.getTitle();
String Views = modell.getViews();
String thumbnail = modell.getThumbnail();
Picasso.get().load(thumbnail).error(R.drawable.common_google_signin_btn_icon_disabled).into(img_discover);
titleofthecontent.setText(Title);
views_discover.setText(Name);
postPosted.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ctx.getApplicationContext(), video.class);
intent.putExtra("title", modell.getTitle());
intent.putExtra("name2", modell.getName2());
intent.putExtra("link", modell.getLink());
intent.putExtra("path", modell.getPath());
intent.putExtra("UID", modell.getUID());
intent.putExtra("videokey", modell.getVideokey());
intent.putExtra("views", modell.getViews());
intent.putExtra("feedkey", modell.getFeedkey());
ctx.startActivity(intent);
}
});
gotoFeed_discover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ctx.getApplicationContext(), feed_pushmycreator.class);
intent.putExtra("feedkey", modell.getFeedkey());
ctx.startActivity(intent);
}
});
}
}
// View Holder for Admob Native Express Ad Unit
public class BannerAdViewHolder extends RecyclerView.ViewHolder {
private AdRequest adRequest;
private AdView adView;
BannerAdViewHolder(View view) {
super(view);
adView = view.findViewById(R.id.banner300x250id);
}
public void setAds(final Context ctx, final all_modell ads){
MobileAds.initialize(ctx.getApplicationContext(), ads.getAdsId()); // "standard app ID to test: ca-app-pub-3940256099942544~3347511713"
// adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
public class HeaderViewHolder extends RecyclerView.ViewHolder {
private Button videosbtn, listiclesbtn, bookbtn;
HeaderViewHolder(View view) {
super(view);
videosbtn = view.findViewById(R.id.Videosdh);
listiclesbtn = view.findViewById(R.id.Listiclesdh);
bookbtn = view.findViewById(R.id.Booksdh);
}
public void setHeaderButtons(Discover_modell discover_modell){
String videosdm = discover_modell.getVideosDm();
String bookdm = discover_modell.getBooksDm();
String listicedm = discover_modell.getListiclesDm();
videosbtn.setText(videosdm);
listiclesbtn.setText(listicedm);
bookbtn.setText(bookdm);
}
}
public static class viewHolderbook extends RecyclerView.ViewHolder {
static TextView booktitle, bookautor;
public static Button bookimg;
public static View postPosted;
Context ctx;
public viewHolderbook(View itemView) {
super(itemView);
this.ctx = ctx;
postPosted = itemView;
booktitle = (TextView) itemView.findViewById(R.id.title_discover);
bookimg = (Button) itemView.findViewById(R.id.gotofeed_discover);
}
public void setBook(final Book book){
String Name = book.getBookautor();
String Title = book.getBooktitle();
booktitle.setText(Title);
}
}
// Constructor for Adapter
public AdmobRecyclerViewAdapter(Context context, List<Object> dataset) {
this.mContext = context;
this.mDataset = dataset;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = 0;
RecyclerView.ViewHolder viewHolder;
// Identify viewType returned by getItemViewType(...)
// and return ViewHolder Accordingly
switch (viewType){
case content:
layout= R.layout.discover_row;
View contentView = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder=new DataViewHolder(contentView);
break;
case book:
layout=R.layout.discover_row;
View bookview = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder=new viewHolderbook(bookview);
break;
case ads:
layout=R.layout.banner300x250;
View header = LayoutInflater
.from(parent.getContext())
.inflate(layout, parent, false);
viewHolder=new BannerAdViewHolder(header);
break;
default:
viewHolder=null;
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType=holder.getItemViewType();
switch (viewType){
case content:
Feed_modell call = (Feed_modell ) mDataset.get(position);
((DataViewHolder)holder).setDateInDiscover(mContext, call);
break;
case book:
Book book = (Book) mDataset.get(position);
((viewHolderbook)holder).setBook(book);
break;
case ads:
all_modell hheaderr = (all_modell) mDataset.get(3);
((BannerAdViewHolder)holder).setAds(mContext, hheaderr);
break;
}
}
@Override
public int getItemCount() {
return mDataset.size();
}
@Override
public int getItemViewType(int position) {
if (mDataset.get(position) instanceof Feed_modell) {
return content;
} else if (mDataset.get(position) instanceof Book) {
return book;
} else if (mDataset.get(3) instanceof all_modell) {
return ads;
}
return -1;
}
---我的活动---
mDataSet.clear();
databaseReference = FirebaseDatabase.getInstance().getReference("feedPosts");
Query queryFeed = databaseReference;
childEventListener = queryFeed.limitToFirst(33).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
for (DataSnapshot value : dataSnapshot.getChildren()){
Feed_modell object = value.getValue(Feed_modell.class);
// getting some data from this child to add to another array
mDataSet.add(object);
mDataSet.add(new all_modell("ca-app-pub3940256099942544~3347511713"));
}
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//databaseReference.addChildEventListener(childEventListener);
}
我不知道为什么,但是没有按我的意愿工作。对于每个正在显示的广告,我都不需要。
答案 0 :(得分:0)
您的问题在viewHolderbook
中
static TextView booktitle, bookautor;
public static Button bookimg;
public static View postPosted;
您已将这些变量定义为static
。这意味着它们是类变量,而不是实例变量,并且它们仅存在一次。删除static
关键字。
第二个问题
这是第二个问题,在getItemViewType()
中:
} else if (mDataset.get(3) instanceof all_modell) {
return ads;
}
很显然,如果mDataset.get(3)
是all_modell
的实例,那么无论ads
的值是什么,它都将返回position
。