Error "Incompatible types" while adding NativeAds in recyclerView

1.4k views Asked by At

I am trying to add Native ads in my recyclerView. For that I am following this tutorial. But looks like my RecyclerViewAdapter is a little bit different than what they have with them.

package com.comp.app.adapters;

public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

private Context context;
private Constants constants;
private String androidId;
private String id;
private ArrayList<CardSetterGetter> arrayList = new ArrayList<CardSetterGetter>();
private ArrayList<String> favouriteItemList = new ArrayList<String>();
private int totalCount;
private static final int MENU_ITEM_VIEW_TYPE = 0 ;
private static final int AD_VIEW_TYPE = 1;

public RecyclerViewAdapter(Context context, ArrayList<CardSetterGetter> arrayList, ArrayList<String> favouriteItemList, int totalCount) {
    this.arrayList = arrayList;
    this.context = context;
    this.arrayList = arrayList;
    this.favouriteItemList = favouriteItemList;
    this.totalCount = totalCount;

}

@Override
public long getItemId(int position) {
    setHasStableIds(true);
    return super.getItemId(position);
}

@Override
public RecyclerViewAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    switch (viewType){

        case AD_VIEW_TYPE:
            View nativeExpressLayoutView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.native_express_ad_container, parent, false);
            return new NativeExpressAdViewHolder(nativeExpressLayoutView); // Incompatible types.  Required: com.comp.app.adapters.RecyclerViewAdapter.RecyclerViewHolder                    Found: com.comp.app.adapters.RecyclerViewAdapter.NativeExpressAdViewHolder

        case MENU_ITEM_VIEW_TYPE:

        default:
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cards, parent, false);
            return new RecyclerViewHolder(view);
    }
}


@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {

    int viewType = getItemViewType(position);

    switch (viewType){

        case AD_VIEW_TYPE:
            NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder)holder; //Inconvertible types;  cannot case 'com.comp.app.adapters.RecyclerViewAdapter.RecyclerViewHolder' to 'com.comp.app.adapters.RecyclerViewAdapter.NativeExpressAdViewHolder'
            NativeExpressAdView adView = (NativeExpressAdView)holder.getPosition(); //Inconvertible types; cannot cast 'int' to 'com.google.android.gms.ads.NativeExpressAdView

            ViewGroup adCardView = (ViewGroup)nativeExpressHolder.itemView;
            adCardView.removeAllViews();

            if(adView.getParent() != null){
                ((ViewGroup)adView.getParent()).removeView(adView);
            }

            adCardView.addView(adView);
            break;

        case MENU_ITEM_VIEW_TYPE:

        default:
            String card_image;
            CardSetterGetter cardSetterGetter;
            cardSetterGetter = arrayList.get(position);

            card_image = constants.SERVERIP.concat(cardSetterGetter.getImageurl());
            holder.cardSetterGetter = cardSetterGetter;
            holder.title.setText(cardSetterGetter.getImagetitle()); //set title

            cardSetterGetter.getTag();

            PointF focusPoint = new PointF(0.5f, 0f);
            // your app populates the focus point

            holder.image // set image
                    .getHierarchy()
                    .setActualImageFocusPoint(focusPoint);
            holder.image.setImageURI(Uri.parse(card_image));

            if (cardSetterGetter.getFavourite().equals("yes")) {
                holder.favourite_button.setImageResource(R.drawable.ic_bookmark_black_36dp);

            } else {
                holder.favourite_button.setImageResource(R.drawable.ic_bookmark_border_black_36dp);
            }
    }

}

public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder{
    NativeExpressAdViewHolder(View view){
        super(view);
    }
}

@Override
public int getItemViewType(int position){
    return (position % 8 == 0)? AD_VIEW_TYPE: MENU_ITEM_VIEW_TYPE;
}

@Override
public int getItemCount() {

    return arrayList.size();
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    TextView title;
    RelativeLayout relativeLayout;
    RelativeLayout cardDescription;
    SimpleDraweeView image;
    //ImageView image;
    ImageView favourite_button;
    CardSetterGetter cardSetterGetter;
    ImageView share;


    private RecyclerViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.image_title);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.activity_dash_board);
        cardDescription = (RelativeLayout) itemView.findViewById(R.id.card_details);
        image = (SimpleDraweeView) itemView.findViewById(R.id.image);
        favourite_button = (ImageView) itemView.findViewById(R.id.favourite_button);
        share = (ImageView) itemView.findViewById(R.id.share);

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Activity activity = (Activity) context;
                saveInfo(constants.SERVERIP.concat(cardSetterGetter.getImageurl()), cardSetterGetter.getImageId(), cardSetterGetter.getDescription(), cardSetterGetter.getImagetitle(), cardSetterGetter.getFavourite());
                Intent i = new Intent().setClass(context, DetailsImageviewActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                context.startActivity(i);
                activity.overridePendingTransition(R.anim.pull_from_right, R.anim.pull_out_to_right);
            }
        });
        favourite_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                favouriteItemList.add(title.getText().toString());

                if (cardSetterGetter.getFavourite().equals("yes")) {
                    id = cardSetterGetter.getImageId();
                    System.out.println("imageid" + id);
                    getData();
                    requestWithSomeHttpHeaders1();
                    System.out.println("header1");
                    favourite_button.setImageResource(R.drawable.ic_bookmark_border_black_36dp);
                    cardSetterGetter.setFavorite("no");

                    Toast.makeText(context, "Bookmark Removed", Toast.LENGTH_SHORT).show();

                } else {

                    id = cardSetterGetter.getImageId();
                    System.out.println("imageid" + id);
                    getData();
                    System.out.println("header2");
                    requestWithSomeHttpHeaders2();
                    favourite_button.setImageResource(R.drawable.ic_bookmark_black_36dp);
                    cardSetterGetter.setFavorite("yes");

                    Toast.makeText(context, "Bookmark Added", Toast.LENGTH_SHORT).show();

                }

            }
        });
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String shareBody = constants.SERVERIP.concat(cardSetterGetter.getImageurl());
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "hey, check out this infographic/fact on app.");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                context.startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));

            }
        });


    }
}

private void getData() {
    try {
        SharedPreferences sharedPref = context.getSharedPreferences("mobileId", Context.MODE_PRIVATE);
        androidId = sharedPref.getString("mobileId", "");
    } catch (Exception e) {

    }


}

private void requestWithSomeHttpHeaders2() {
    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest postRequest = new StringRequest(Request.Method.POST, "My URL",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    System.out.println(response);
                }


            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    Log.d("ERROR", "error => " + error.toString());
                    NetworkResponse networkResponse = error.networkResponse;
                    if (networkResponse != null) {
                        Log.e("Volley", "Error. HTTP Status Code:" + networkResponse.statusCode);
                    }

                    if (error instanceof TimeoutError) {
                        Log.e("Volley", "TimeoutError");
                    } else if (error instanceof NoConnectionError) {
                        Log.e("Volley", "NoConnectionError");
                    } else if (error instanceof AuthFailureError) {
                        Log.e("Volley", "AuthFailureError");
                    } else if (error instanceof ServerError) {
                        Log.e("Volley", "ServerError");
                    } else if (error instanceof NetworkError) {
                        Log.e("Volley", "NetworkError");
                    } else if (error instanceof ParseError) {
                        Log.e("Volley", "ParseError");
                    }
                }


            }

    )

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            // return super.getParams();
            Map<String, String> params = new HashMap<String, String>();
            params.put("device_id", androidId);
            params.put("image_id", id);
            params.put("favourite", "yes");

            return params;

        }
    };
    queue.add(postRequest);
}

private void requestWithSomeHttpHeaders1() {
    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest postRequest = new StringRequest(Request.Method.POST, "My URL",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {


                    System.out.println(response);
                }


            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    Log.d("ERROR", "error => " + error.toString());
                    NetworkResponse networkResponse = error.networkResponse;
                    if (networkResponse != null) {
                        Log.e("Volley", "Error. HTTP Status Code:" + networkResponse.statusCode);
                    }

                    if (error instanceof TimeoutError) {
                        Log.e("Volley", "TimeoutError");
                    } else if (error instanceof NoConnectionError) {
                        Log.e("Volley", "NoConnectionError");
                    } else if (error instanceof AuthFailureError) {
                        Log.e("Volley", "AuthFailureError");
                    } else if (error instanceof ServerError) {
                        Log.e("Volley", "ServerError");
                    } else if (error instanceof NetworkError) {
                        Log.e("Volley", "NetworkError");
                    } else if (error instanceof ParseError) {
                        Log.e("Volley", "ParseError");
                    }
                }


            }

    )

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("device_id", androidId);
            params.put("image_id", id);
            params.put("favourite", "no");

            return params;

        }
    };
    queue.add(postRequest);
}

private void saveInfo(String imageUrl, String imageId, String imageDescription, String imageTitle, String favourite) {
    SharedPreferences sharedPref = context.getSharedPreferences("ImageInfo", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("imageUrl", imageUrl);
    editor.putString("imageId", imageId);
    editor.putString("imageDescription", imageDescription);
    editor.putString("imageTitle", imageTitle);
    editor.putString("favourite", favourite);
    editor.apply();


}


}

This is the code in my Activity

private List listItems;

private void addNativeExpressAds(){
for (int i = 0; i<listItems.size(); i += 5){
    NativeExpressAdView adView = new NativeExpressAdView(this);
    adView.setAdSize(new AdSize(320,150));
    adView.setAdUnitId("My adUnitId");
    adView.loadAd(new AdRequest.Builder().build());
    listItems.add(i,adView); //wrong 2nd argument type, 'com.google.android.gms.ads.NativeExpressAdView', required:'app.appname.CardSetterGetter'
}
}
0

There are 0 answers