Я инициализирую баннерную рекламу в RecyClerview, но я сталкиваюсь с ошибкой, посмотрел на ошибку, лежал это «java.lang.classcastexception: com.app.sliderandrecyclerview.rcmodel не может быть выбран в com.google.android.gms.ads.adview at com.app. sliderandrecyclerview.rcholder.onbindviewholder (rcholder.java:59) "

Rcview.java.

    setContentView(R.layout.activity_recycler_view);

    recyclerView = findViewById(R.id.recycelerview);
    tDatabase = FirebaseDatabase.getInstance();
    tReference = tDatabase.getReference("TERM");
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    MobileAds.initialize(this,"ca-app-pub- 
    3940256099942544~3347511713");

    tReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot 
         dataSnapshot) {

            for (DataSnapshot qu : dataSnapshot.getChildren()){
                RcModel v = qu.getValue(RcModel.class);
                rcList.add(v);
            }
           adapter = new RcHolder(getApplicationContext(),rcList);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

        @Override
     public void onCancelled(@NonNull DatabaseError databaseError) 
     {
    Toast.makeText(getApplicationContext(),"Check your Internet 
    Connection",Toast.LENGTH_SHORT).show();
        }
    });
    Item();
    getBannerAds();
    loadBannerAds();
   }

 private void Item(){
    List<RcModel> rcmodel = new ArrayList<>();

    for (RcModel rc : rcmodel){
        rcList.add(rc);
    }
  }

 private void getBannerAds(){
    for (int i = 0; i<rcList.size(); i+= ITEM_PER_ADS){
        final AdView adView = new AdView(RCVIEW.this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(ADS);
        rcList.add(i,adView);
    }
  }

  private void loadBannerAds(){
    for (int i = 0; i<rcList.size(); i++){
        Object item = rcList.get(i);
        if (item instanceof AdView){
            final AdView adView = (AdView) item;
            adView.loadAd(new AdRequest.Builder().build());
        }
       }
     }
     }

Rcview.xml.

   <androidx.constraintlayout.widget.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".RCVIEW">


  <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycelerview"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

View.xml

 <androidx.cardview.widget.CardView 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="10dp"
 app:cardCornerRadius="5dp"
 android:padding="10dp"
 app:cardElevation="10dp"
 app:cardMaxElevation="5dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_margin="10dp"
        android:text="Termname"
        android:textAlignment="center"
        android:textColor="#ffff"
        android:textStyle="bold"/>

    <TextView
        android:textStyle="bold"
        android:id="@+id/detail"
        android:layout_below="@id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:text="Lorem Ipsum"
        android:padding="5dp"
        android:textAlignment="center"/>

</RelativeLayout>
</androidx.cardview.widget.CardView>

Banner_ads.xml.

 <androidx.cardview.widget.CardView 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:id="@+id/ads"
 android:layout_height="wrap_content"
 app:cardCornerRadius="5dp"
 android:padding="10dp"
 app:cardElevation="10dp"
 app:cardMaxElevation="5dp">
 </androidx.cardview.widget.CardView>

RCHOLDER.JAVA

public static final int rc_item = 0;
public static final int rc_ads = 1;

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull 
ViewGroup parent, int viewType) {
    switch (viewType) {
        case rc_item:
            return new 
 MyViewHolder(LayoutInflater.from(ctx).inflate(R.layout.view, 
 parent, false));
        case rc_ads:

        default:
            return new 
AdViewHolder(LayoutInflater.from(ctx).inflate(R.layout.bannar_ads, 
 parent, false));
    }
  }

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder 
holder, int position) {

    int viewType = getItemViewType(position);
    switch (viewType){
        case rc_item:
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            RcModel rcModel = (RcModel) objectList.get(position);
            myViewHolder.title.setText(rcModel.getTitle());
            myViewHolder.detail.setText(rcModel.getDetail());
        case rc_ads:

        default:
            AdViewHolder adViewHolder = (AdViewHolder) holder;
            AdView adView = (AdView) objectList.get(position);
            ViewGroup adcardView = (ViewGroup) 
       adViewHolder.itemView;
            if (adcardView.getChildCount() > 0){
                adcardView.removeAllViews();
            }
            if (adcardView.getParent()!=null){
                ((ViewGroup) 
   adView.getParent()).removeView(adView);
            }
            adcardView.addView(adView);
    }

}

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

@Override
public int getItemViewType(int position) {
    if (position%RCVIEW.ITEM_PER_ADS == 0){
        return rc_ads;
    }
    return rc_item;
}

class MyViewHolder extends RecyclerView.ViewHolder{

    public TextView title;
    public TextView detail;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        title = itemView.findViewById(R.id.title);
        detail = itemView.findViewById(R.id.detail);
    }
   }

    class AdViewHolder extends RecyclerView.ViewHolder{

    public AdViewHolder(@NonNull View itemView) {
        super(itemView);
    }

Как решить ошибку?

0
user10997009 23 Сен 2019 в 18:51

1 ответ

Лучший ответ

Обновление, я решаю эту проблему, просто инициализируйте этот метод 3 в

Treference.addvalueeventListener (новый valueeventlistener () {@ @ override public void ondatachange (@nonnull datasnapshot datasnapshot) {

        for (DataSnapshot qu : dataSnapshot.getChildren()){
            RcModel v = qu.getValue(RcModel.class);
            rcList.add(v);
        }
        Item();
        getBannerAds();
        loadBannerAds();
       adapter = new RcHolder(getApplicationContext(),rcList);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
0
user10997009 25 Сен 2019 в 17:29