0

Admob garageチュートリアルに従って、RecyclerViewにネイティブ広告を実装しようとしています。しかし、「エラー:互換性のないタイプ:ヒットをNativeExpressAdViewに変換できません」エラー:互換性のないタイプ:RecyclerViewに変換できません

setUpAndLoadNativeExpressAdsで、「hit.get(i)」から「NativeExpressAdView」をキャストすることは冗長です。 "ここ

(エラーがベターusderstandingコメント// ERROR行と行//上onBindViewHolderである。)

ここでは、私のクラスである

package com.example.wallpaper.app.ui.adapter; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 
import com.example.wallpaper.app.model.Hit; 
import com.example.wallpaper.app.model.Pic; 
import com.example.wallpaper.app.ui.DiscoverFragment; 
import com.example.wallpaper.app.ui.PicDetail; 
import com.example.wallpaper.app.R; 
import com.example.wallpaper.app.ui.util.WallpaperViewHolder; 
import com.google.android.gms.ads.NativeExpressAdView; 
import com.squareup.picasso.Picasso; 
import java.util.ArrayList; 
import java.util.List; 

public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperViewHolder> { 

private List<Hit> hit; 
private Context context; 
public int width; 
public int height; 
public static final int IMAGE__LIST_ITEMS = 0; 
public static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1; 

public WallpaperAdapter (Context context){ 
    this.context=context; 
    this.hit=new ArrayList<>(); 
} 

public class NativeExpressAdViewHolder extends WallpaparViewHolder { 

    NativeExpressAdViewHolder(View view) { 
     super(view); 
    } 
} 

@Override 
public int getItemViewType(int position) 
{ 
    return (position % DiscoverFragment.ITEMS_PER_AD == 0)? NATIVE_EXPRESS_AD_VIEW_TYPE : IMAGE__LIST_ITEMS; 
} 

@Override 
public int getItemCount() 
{ 
    return (hit == null) ? 0 : hit.size(); 
} 

@Override 
public WallpaperViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 
    switch (viewType){ 
     case IMAGE__LIST_ITEMS: 
      View itemView = LayoutInflater.from(context).inflate(R.layout.wallp_item, null); 
      final WallpaperViewHolder rcv=new WallpaperViewHolder(itemView); 
      itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        int position = rcv.getAdapterPosition(); 
        Intent intent = new Intent(context, PicDetail.class); 
        intent.putExtra(PicDetail.EXTRA_PIC, hit.get(position)); 
        context.startActivity(intent); 
       } 
      }); 
      return rcv; 

     case NATIVE_EXPRESS_AD_VIEW_TYPE: 
     default: 
      View nativeExpressLayoutView = LayoutInflater.from(
        viewGroup.getContext()).inflate(R.layout.native_express_ad_container, 
        viewGroup, false); 
      return new WallpaperViewHolder(nativeExpressLayoutView); 
    } 
} 

@Override 
public void onBindViewHolder(WallpaperViewHolder holder, int position) { 
    int viewType = getItemViewType(position); 
    switch (viewType){ 

     case IMAGE__LIST_ITEMS: 
      Hit photo = this.hit.get(position); 
      RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) holder.discWallp.getLayoutParams(); 
      float height =photo.getPreviewHeight(); 
      float width = photo.getPreviewWidth(); 
      float ratio = height/width ; 
      rlp.height = (int) (rlp.width * ratio); 
      holder.discWallp.setLayoutParams(rlp); 
      holder.discWallp.setRatio(ratio); 
      Picasso.with(context) 
        .load(photo.getWebformatURL()) 
        .placeholder(R.drawable.plh) 
        .into(holder.discWallp); 
      break; 

     case NATIVE_EXPRESS_AD_VIEW_TYPE: 
     default: 

     **// ERROR Lines// // ERROR Lines// // ERROR Lines// // ERROR Lines//** 

      NativeExpressAdViewHolder nativeExpressHolder; 
      nativeExpressHolder = (NativeExpressAdViewHolder) holder; 
      NativeExpressAdView adView; 
      adView = (NativeExpressAdView) hit.get(position); 

     **// ERROR Lines// // ERROR Lines// // ERROR Lines// // ERROR Lines//** 

      ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView; 
      if (adCardView.getChildCount() > 0) { 
       adCardView.removeAllViews(); 
      } 
      if (adView.getParent() != null) { 
       ((ViewGroup) adView.getParent()).removeView(adView); 
      } 
      adCardView.addView(adView); 
    } 
} 

public void setPicList(Pic picList) 
{ 
    if(picList.getHits()!=null) 
     this.hit.addAll(picList.getHits()); 
    notifyDataSetChanged(); 
} 
} 

断片

public class DiscoverFragment extends Fragment implements AsyncResponse { 

public WallpaperAdapter WallpaperAdapter ; 
public RecyclerView recyclerView; 
public NetworkUtilities networkUtilities; 
public EndlessRecyclerViewScrollListener scrollListener; 
public WallpService wallpService; 
public int column_no; 
public static final int ITEMS_PER_AD = 4; 
// List of Native Express ads and MenuItems that populate the RecyclerView. 
private ArrayList<Object> hit = new ArrayList<>(); 
private static final int NATIVE_EXPRESS_AD_HEIGHT = 150; 
// The Native Express ad unit ID. 
private static final String AD_UNIT_ID = "ca-app-pub-xxxxxxxxxx543"; 

public static DiscoverFragment newInstance() { 
    DiscoverFragment fragment = new DiscoverFragment(); 
    Bundle args = new Bundle(); 
    fragment.setArguments(args); 
    return fragment; 
} 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    networkUtilities = new NetworkUtilities(getActivity()); 

    recyclerView = (RecyclerView) getActivity().findViewById(R.id.discRecView); 
    recyclerView.setHasFixedSize(true); 

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
    recyclerView.setLayoutManager(layoutManager); 

    loadNextDataFromApi(1); 
    addNativeExpressAds(); 
    setUpAndLoadNativeExpressAds(); 
} 

private void addNativeExpressAds() { 

    // Loop through the items array and place a new Native Express ad in every ith position in 
    // the items List. 
    for (int i = 0; i <= hit.size(); i += ITEMS_PER_AD) { 
     final NativeExpressAdView adView = new NativeExpressAdView(getActivity() 
       .getApplicationContext()); 
     hit.add(i, adView); 
    } 
} 

private void setUpAndLoadNativeExpressAds() { 
    // Use a Runnable to ensure that the RecyclerView has been laid out before setting the 
    // ad size for the Native Express ad. This allows us to set the Native Express ad's 
    // width to match the full width of the RecyclerView. 
    recyclerView.post(new Runnable() { 
     @Override 
     public void run() { 
      final float scale = getActivity().getApplicationContext() 
        .getResources().getDisplayMetrics().density; 

      // Set the ad size and ad unit ID for each Native Express ad in the items list. 
      for (int i = 0; i <= hit.size(); i += ITEMS_PER_AD) { 
       final NativeExpressAdView adView = 
         (NativeExpressAdView) hit.get(i); 
       final CardView cardView = (CardView) getActivity().findViewById(R.id.ad_card_view); 
       final int adWidth = cardView.getWidth() - cardView.getPaddingLeft() 
         - cardView.getPaddingRight(); 
       AdSize adSize = new AdSize((int) (adWidth/scale), NATIVE_EXPRESS_AD_HEIGHT); 
       adView.setAdSize(adSize); 
       adView.setAdUnitId(AD_UNIT_ID); 
      } 

      // Load the first Native Express ad in the items list. 
      loadNativeExpressAd(0); 
     } 
    }); 
} 

private void loadNativeExpressAd(final int index) { 

    if (index >= hit.size()) { 
     return; 
    } 

    Object item = hit.get(index); 
    if (!(item instanceof NativeExpressAdView)) { 
     throw new ClassCastException("Expected item at index " + index + " to be a Native" 
       + " Express ad."); 
    } 

    final NativeExpressAdView adView = (NativeExpressAdView) item; 

    // Set an AdListener on the NativeExpressAdView to wait for the previous Native Express ad 
    // to finish loading before loading the next ad in the items list. 
    adView.setAdListener(new AdListener() { 
     @Override 
     public void onAdLoaded() { 
      super.onAdLoaded(); 
      // The previous Native Express ad loaded successfully, call this method again to 
      // load the next ad in the items list. 
      loadNativeExpressAd(index + ITEMS_PER_AD); 
     } 

     @Override 
     public void onAdFailedToLoad(int errorCode) { 
      // The previous Native Express ad failed to load. Call this method again to load 
      // the next ad in the items list. 
      Log.e("MainActivity", "The previous Native Express ad failed to load. Attempting to" 
        + " load the next Native Express ad in the items list."); 
      loadNativeExpressAd(index + ITEMS_PER_AD); 
     } 
    }); 

    // Load the Native Express ad. 
    adView.loadAd(new AdRequest.Builder().build()); 
} 


@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    if(!networkUtilities.isInternetConnectionPresent()){ 
     return inflater.inflate(R.layout.fragment_no_internet, container, false); 
    } 
    else{ 
     View view = inflater.inflate(R.layout.fragment_discover, container, false); 
     recyclerView=(RecyclerView) view.findViewById(R.id.discRecView); 
     recyclerView.setHasFixedSize(true); 
     checkScreenSize(); 
     StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(column_no,StaggeredGridLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(staggeredGridLayoutManager); 
     scrollListener = new EndlessRecyclerViewScrollListener(staggeredGridLayoutManager) { 
      @Override 
      public void onLoadMore(int page, int totalItemsCount, RecyclerView view) { 
       loadNextDataFromApi(page); 
      } 
     }; 
     scrollListener.resetState(); 
     recyclerView.addOnScrollListener(scrollListener); 
     WallpaperAdapter = new WallpaperAdapter (getActivity()); 
     recyclerView.setAdapter(WallpaperAdapter); 
     return view; 
    } 
} 

@Override 
public void processFinish(Pic output) { 

    if (output.getHits() != null) { 
     WallpaperAdapter .setPicList(output); 
    } 

} 

public void loadNextDataFromApi(int offset) { 
    String type=getResources().getString(R.string.popular); 
    wallpService = new WallpService(networkUtilities, getActivity(), this,offset,type); 
    wallpService.loadWallp(); 
} 
public void checkScreenSize() { 

    int screenSize = getResources().getConfiguration().screenLayout & 
      Configuration.SCREENLAYOUT_SIZE_MASK; 

    switch (screenSize) { 
     case Configuration.SCREENLAYOUT_SIZE_XLARGE: 

      column_no = 3; 
      break; 
     case Configuration.SCREENLAYOUT_SIZE_UNDEFINED: 
      column_no = 2; 
      break; 
     case Configuration.SCREENLAYOUT_SIZE_LARGE: 
      column_no = 2; 
      break; 
     case Configuration.SCREENLAYOUT_SIZE_NORMAL: 
      column_no = 1; 
      break; 
     case Configuration.SCREENLAYOUT_SIZE_SMALL: 
      column_no = 1; 
      break; 
     default: 
      column_no = 1; 
    } 
} 
} 

答えて

0

あなたhit変数はList<Hit>です。だから明らかにそれにはHitのオブジェクトしかありません。

このコードは:

adView = (NativeExpressAdView) hit.get(position); 

そのオブジェクトはおそらくNativeExpressAdViewすることはできませんので、失敗する予定です。

リストをObjectのタイプにし、NativeExpressAdViewオブジェクトの広告を実際の方法と同じようにする必要があります。here私はこの例をダウンロードして演奏し、それを動かすためにあなたのコードに移動することをお勧めします。

+0

私はNativeExpressAdViewHolderを解決しました。私はwallpaper.HolderをRecycle.ViewHolderの代わりに使用しました。しかし、2番目のものはまだ鋳造に問題があります。私は何が間違っているのか理解できません。私はコードを更新しました。質問を確認してください。 –

+0

NativeExpressRecyclerViewExampleに続いて、コードに[addNativeExpressAds()](https://github.com/googleads-mobile-android-examples/blob/master/advanced/NativeExpressRecyclerViewExample/app/src/main/java)が表示されません/com/google/android/gms/example/nativeexpressrecyclerviewexample/MainActivity.java#L80)メソッド。あなたの 'WallpaperAdapter'クラスは、' List 'の代わりにリサイクラのビューアイテムに' List 'をまだ使っています。実際にあなたのリストにネイティブ表現オブジェクトはありません。 –

+0

私は自分の断片に完全なコードを掲載しました。質問を確認してください –

関連する問題