我有一个工作RecyclerView
,但后来我决定添加谷歌原生广告!
所以我确实改变了RecyclerViewAdapter
类似谷歌开发者指南!
它说列表必须是对象列表(List<Object>)
所以我改变了这个和适配器中的其他一些东西!
为了超越我"List<DataForTextRow>"
,我只是将整个列表投射为对象!
customRVAdapterIndicator = new CustomRVAdapterCompareCountriesByIndicator(this.getActivity(), new ArrayList<Object>(listAdapter));
然后 Android Studio 显示没有错误,我能够启动应用程序。但是,当我要打开上面提到的 Activity 时,RecyclerView
它会崩溃。
它说:
DataForTextRow 无法转换为 com.google.android.gms.ads.NativeExpressAdView
这是以下行onBindViewHolder
NativeExpressAdView adView = (NativeExpressAdView)dataForTextRowList.get(position);
因此,为了测试,我只是在 onBindViewHolder 中评论了整个案例 AD_VIEW_TYPE
然后我得到了错误:
NativeExpressAdViewHolder cannot be cast to ....adapterr.CustomRVAdapterCompareCountriesByIndicator$IndicatorViewHolder
这是在以下行中:
IndicatorViewHolder indicatoViewHolder = (IndicatorViewHolder)( holder);
有没有人有类似的问题?
提前致谢!.
public class CustomRVAdapterCompareCountriesByIndicator extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Object> dataForTextRowList;
private static Context mContext;
private static final int MENU_ITEM_VIEW_TYPE=0;
private static final int AD_VIEW_TYPE=1;
public CustomRVAdapterCompareCountriesByIndicator(Context mContext, List<Object> dataForTextRowList) {
this.mContext=mContext;
this.dataForTextRowList = dataForTextRowList;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
switch (position){
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.native_express_ad_container,parent,false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
default:
View rowViewDetail = LayoutInflater.from(parent.getContext())
.inflate(R.layout.each_list_item_compare_countries,parent,false);
IndicatorViewHolder indicatorViewHolder=new IndicatorViewHolder(rowViewDetail);
return indicatorViewHolder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final int pos=position;
int viewType = getItemViewType(position);
switch (viewType){
case AD_VIEW_TYPE:
NativeExpressAdViewHolder nativeExpressHolder= (NativeExpressAdViewHolder)holder;
NativeExpressAdView adView = (NativeExpressAdView)dataForTextRowList.get(position);
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:
DecimalFormat formatter = new DecimalFormat("#,###,###.####");
IndicatorViewHolder indicatoViewHolder = (IndicatorViewHolder)((RecyclerView.ViewHolder) holder);
DataForTextRow item = (DataForTextRow) dataForTextRowList.get(position);
indicatoViewHolder.name.setText(((DataForTextRow)dataForTextRowList.get(position)).getTitle());
if (!((DataForTextRow)dataForTextRowList.get(position)).getTitleValue().equals("n.a.") ){
try {
indicatoViewHolder.value.setText(formatter.format( Double.valueOf( ((DataForTextRow)dataForTextRowList.get(position)).getTitleValue())) );
} catch (NumberFormatException e) {
e.printStackTrace();
}
}else{
indicatoViewHolder.value.setText(((DataForTextRow)dataForTextRowList.get(position)).getTitleValue());
}
indicatoViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(mContext, CompareCountriesListActivity.class);
i.putExtra("indicatorId",((DataForTextRow)dataForTextRowList.get(pos)).getIndicatorId());
i.putExtra("description", ((DataForTextRow)dataForTextRowList.get(pos)).getTitleDescription());
i.putExtra("title",((DataForTextRow)dataForTextRowList.get(pos)).getTitle());
i.putExtra("sourceOrganization",((DataForTextRow)dataForTextRowList.get(pos)).getSourceOrganization());
i.putExtra("topicNr",((DataForTextRow)dataForTextRowList.get(pos)).getTopicNr());
mContext.startActivity(i);
}
});
indicatoViewHolder.infoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext, DetailsDescriptionPop.class);
i.putExtra("description", ((DataForTextRow)dataForTextRowList.get(pos)).getTitleDescription());
i.putExtra("title",((DataForTextRow)dataForTextRowList.get(pos)).getTitle());
i.putExtra("sourceOrganization",((DataForTextRow)dataForTextRowList.get(pos)).getSourceOrganization());
mContext.startActivity(i);
}
});
}
}
@Override
public int getItemCount() {
return dataForTextRowList.size();
}
@Override
public int getItemViewType(int position) {
return (position%8==0) ? AD_VIEW_TYPE: MENU_ITEM_VIEW_TYPE;
}
public class IndicatorViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView value;
ImageButton infoBtn;
public IndicatorViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.name);
infoBtn = (ImageButton) itemView.findViewById(R.id.infoBtn);
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder{
NativeExpressAdViewHolder(View view){
super(view);
}
}
}