问题
I was following a tutorial for NativeExpressAdView inside RecyclerView from this link which was provided by google. In this tutorial List was taken as Object Model Class. But my Recylerview has my own custom Model class name as: Artical. That's why I am doing some changes in my code. My code is below.
ArticleAdapter.java
package com.prof.rssparser.example;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.ads.NativeExpressAdView;
import com.prof.rssparser.Article;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static com.prof.rssparser.example.R.drawable.placeholder;
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
private List <Article> articles;
private int rowLayout;
private Context mContext;
WebView articleView;
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;
public ArticleAdapter(List<Article> list, int rowLayout, Context context)
{
this.articles = list;
this.rowLayout = rowLayout;
this.mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView pubDate;
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
pubDate = (TextView) itemView.findViewById(R.id.pubDate);
image = (ImageView)itemView.findViewById(R.id.image);
}
}
public class NativeExpressAdViewHolder extends ArticleAdapter.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
@Override
public int getItemCount() {
return articles == null ? 0 : articles.size();
}
@Override
public int getItemViewType(int position) {
return (position % 8 == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
: MENU_ITEM_VIEW_TYPE;
}
@Override
public long getItemId(int item) {
// TODO Auto-generated method stub
return item;
}
public void clearData(){
if (articles != null)
articles.clear();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
switch (i)
{
case MENU_ITEM_VIEW_TYPE:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
case NATIVE_EXPRESS_AD_VIEW_TYPE:
// fall through
default:
View nativeExpressLayoutView = LayoutInflater.from(
viewGroup.getContext()).inflate(R.layout.row,
viewGroup, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
}
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
Article currentArticle = articles.get(position);
Locale.setDefault(Locale.getDefault());
Date date = currentArticle.getPubDate();
SimpleDateFormat sdf = new SimpleDateFormat();
sdf = new SimpleDateFormat("dd MMMM yyyy");
final String pubDateString = sdf.format(date);
viewHolder.title.setText(currentArticle.getTitle());
Picasso.with(mContext)
.load(currentArticle.getImage())
.placeholder(placeholder)
.fit()
.centerCrop()
.into(viewHolder.image);
viewHolder.pubDate.setText(pubDateString);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//show article content inside a dialog
articleView = new WebView(mContext);
articleView.getSettings().setLoadWithOverviewMode(true);
String title = articles.get(position).getTitle();
String content = articles.get(position).getContent();
articleView.getSettings().setJavaScriptEnabled(true);
articleView.setHorizontalScrollBarEnabled(false);
articleView.setWebChromeClient(new WebChromeClient());
articleView.loadDataWithBaseURL(null, "<style>img{display: inline; height: auto; max-width: 100%;} " +
"</style>\n" + "<style>iframe{ height: auto; width: auto;}" + "</style>\n" + content, null, "utf-8", null);
android.support.v7.app.AlertDialog alertDialog = new android.support.v7.app.AlertDialog.Builder(mContext).create();
alertDialog.setTitle(title);
alertDialog.setView(articleView);
alertDialog.setButton(android.support.v7.app.AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
((TextView) alertDialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
}
});
break;
case NATIVE_EXPRESS_AD_VIEW_TYPE:
// fall through
default:
NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder) viewHolder;
//i know below casting is wrong but what is right method for this?
NativeExpressAdView adView = (NativeExpressAdView)(Object)articles.get(position);
ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
// Add the Native Express ad to the native express ad view.
adCardView.addView(adView);
}
}
}
Artical.java
package com.prof.rssparser;
import java.util.Date;
public class Article {
private String title;
private String author;
private String link;
private Date pubDate;
private String description;
private String content;
private String image;
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getLink() {
return link;
}
public Date getPubDate() {
return pubDate;
}
public String getDescription() {
return description;
}
public String getContent() {
return content;
}
public String getImage() {
return image;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setLink(String link) {
this.link = link;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public void setDescription(String description) {
this.description = description;
}
public void setContent(String content) {
this.content = content;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Article{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", link='" + link + '\'' +
", pubDate=" + pubDate +
", description='" + description + '\'' +
", content='" + content + '\'' +
", image='" + image + '\'' +
'}';
}
}
I get this error:
java.lang.ClassCastException: com.prof.rssparser.Article cannot be cast to com.google.android.gms.ads.NativeExpressAdView
at com.prof.rssparser.example.ArticleAdapter.onBindViewHolder(ArticleAdapter.java:201)
at com.prof.rssparser.example.ArticleAdapter.onBindViewHolder(ArticleAdapter.java:48)
As you can see I Cast Object
to Artical
by (Object)articles.get(position)
. Is it wrong to use Casting or is there any other way to approach this? How can I add NativeExpressAdView with Custom Model List. Please help me and sorry for bad formatting, I am a noob.
来源:https://stackoverflow.com/questions/45913734/how-to-add-nativeexpressadview-with-custom-model-list-android