- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用 RecyclerView 添加 Admob 横幅广告时出现 ClassCastException。我正在使用来自 blogger 的 JSON 数据,在添加横幅广告之前它运行良好。我正在使用来自 here 的 GitHub 项目引用.
Error : com.example.abcd.CategoryItem cannot be cast to com.google.android.ads.AdView at com.example.abcd.CategoryAdapter.onBindViewHolder (CategoryAdapter.java:123)
这是我的类别 Activity
public class CategoryActivity extends AppCompatActivity {
public static final int ITEMS_PER_AD=8;
private static final String AD_UNIT_ID="ca-app-pub-3940256099942544/6300978111";
private RecyclerView mRecyclerView;
private CategoryAdapter mCategoryAdapter;
// List of banner ads and MenuItems that populate the RecyclerView.
private List<Object> mCategoryList = new ArrayList<>();
private RequestQueue mRequestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
mRecyclerView = findViewById(R.id.recycler_view_category);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
addBannerAds();
loadBannerAds();
// Specify an adapter.
RecyclerView.Adapter<RecyclerView.ViewHolder> adapter = new CategoryAdapter(this, mCategoryList);
mRecyclerView.setAdapter(adapter);
}
private void parseJSON() {
String url="http://sambalpurijokeszone.blogspot.com/feeds/posts/summary?alt=json&max-results=0";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject feed=(JSONObject) response.get("feed");
JSONArray category =(JSONArray)feed.get("category");
JSONArray sortedCategory=new JSONArray();
List<JSONObject> jsonValues=new ArrayList<JSONObject>();
for (int i = 0; i < category.length(); i++) {
jsonValues.add(category.getJSONObject(i));
}
//Sorting categories
Collections.sort(jsonValues, new Comparator<JSONObject>() {
private static final String KEY_NAME="term";
@Override
public int compare(JSONObject categoryItem, JSONObject t1) {
String valA=new String();
String valB =new String();
try{
valA=(String) categoryItem.get(KEY_NAME);
valB=(String) t1.get(KEY_NAME);
}
catch (JSONException e){
}
return valA.compareToIgnoreCase(valB);
}
});
for (int i = 0; i < category.length(); i++) {
sortedCategory.put(jsonValues.get(i));
}
for (int i = 0; i < sortedCategory.length(); i++) {
JSONObject hit = sortedCategory.getJSONObject(i);
String categoryItem=hit.getString("term");
mCategoryList.add(new CategoryItem(categoryItem));
}
/*mCategoryAdapter = new CategoryAdapter(CategoryActivity.this, mCategoryList);
mRecyclerView.setAdapter(mCategoryAdapter);
mCategoryAdapter.setOnItemClickListener(CategoryActivity.this);*/
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
private void addBannerAds(){
//Loop through the items array and place a new banner ad in every ith position in the item list
for(int i=0;i<=mCategoryList.size();i+= ITEMS_PER_AD){
final AdView adView = new AdView (CategoryActivity.this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
mCategoryList.add(i,adView);
}
}
// Sets up and loads the banner ads.
private void loadBannerAds() {
// Load the first banner ad in the items list (subsequent ads will be loaded automatically
// in sequence).
loadBannerAd(0);
}
//Loads the banner ads in the items list.
private void loadBannerAd(final int index) {
if (index >= mCategoryList.size()) {
return;
}
Object item = mCategoryList.get(index);
if (!(item instanceof AdView)) {
throw new ClassCastException("Expected item at index " + index + " to be a banner ad"
+ " ad.");
}
final AdView adView = (AdView) item;
// Set an AdListener on the AdView to wait for the previous banner 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 banner ad loaded successfully, call this method again to
// load the next ad in the items list.
loadBannerAd(index + ITEMS_PER_AD);
}
@Override
public void onAdFailedToLoad(int errorCode) {
// The previous banner ad failed to load. Call this method again to load
// the next ad in the items list.
Log.e("MainActivity", "The previous banner ad failed to load. Attempting to"
+ " load the next banner ad in the items list.");
loadBannerAd(index + ITEMS_PER_AD);
}
});
// Load the banner ad.
adView.loadAd(new AdRequest.Builder().build());
}
类别适配器:
public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// A menu item view type.
private static final int MENU_ITEM_VIEW_TYPE = 0;
// The banner ad view type.
private static final int BANNER_AD_VIEW_TYPE = 1;
private Context mContext;
// The list of banner ads and category items.
private final List<Object> mCategoryList;
public CategoryAdapter(Context context, List<Object> CategoryList) {
mContext = context;
mCategoryList = CategoryList;
}
@Override
public int getItemCount() {
return mCategoryList.size();
}
public class CategoryViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewCategory;
public CategoryViewHolder(View itemView) {
super(itemView);
mTextViewCategory = itemView.findViewById(R.id.text_view_category);
}
}
public class AdViewHolder extends RecyclerView.ViewHolder {
AdViewHolder(View view) {
super(view);
}
}
/* Determines the view type for the given position*/
@Override
public int getItemViewType(int position) {
if (position % CategoryActivity.ITEMS_PER_AD == 0)
return BANNER_AD_VIEW_TYPE;
else
return MENU_ITEM_VIEW_TYPE;
}
/* Creates a new view for a menu item view or a banner ad view
* based on the viewType. This method is invoked by the layout manager*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.category_item, viewGroup, false);
return new CategoryViewHolder(menuItemLayoutView);
case BANNER_AD_VIEW_TYPE:
// fall through
default:
View bannerLayoutView = LayoutInflater.from(
viewGroup.getContext()).inflate(R.layout.banner_ad_container,
viewGroup, false);
return new AdViewHolder(bannerLayoutView);
}
}
/** Replaces the content in the views that make up the menu item view and the
* * banner ad view. This method is invoked by the layout manager.*/
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
CategoryViewHolder categoryItemHolder = (CategoryViewHolder) holder;
CategoryItem currentItem = (CategoryItem) mCategoryList.get(position);
String categoryName=currentItem.getCategoryName();
categoryItemHolder.mTextViewCategory.setText(categoryName);
break;
case BANNER_AD_VIEW_TYPE:
// fall through
default:
AdViewHolder bannerHolder = (AdViewHolder) holder;
AdView adView = (AdView) mCategoryList.get(position);
ViewGroup adCardView = (ViewGroup) bannerHolder.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
// Add the banner ad to the ad view.
adCardView.addView(adView);
}
}
请帮我解决这个问题。提前致谢。
最佳答案
@Override public int getItemViewType(int position) {
if (mCategoryList.get(position) instanceof AdView)
return BANNER_AD_VIEW_TYPE;
else return MENU_ITEM_VIEW_TYPE;
}
关于java - 添加 admob 横幅广告时出现 RecyclerView ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56815794/
我创建了一个用户可以添加测试的字段。这一切运行顺利我只希望当用户点击(添加另一个测试)然后上一个(添加另一个测试)删除并且这个显示在新字段中。 所有运行良好的唯一问题是点击(添加另一个字段)之前添加另
String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在努力将 jQuery 滚动功能添加到 nav-tab (Bootstrap 3)。我希望用户能够选择他们想要的选项卡,并在选项卡内容中有一个可以平滑滚动到 anchor 的链接。这是我的代码,可
我正在尝试在用户登录后再添加 2 个 ui 选项卡。首先,我尝试做一个之后。 $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
我有一个包含选择元素的表单,我想通过选择添加和删除其中一些元素。这是html代码(这里也有jsfiddle http://jsfiddle.net/txhajy2w/):
正在写这个: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) 等同于: view.backgroundColor = UICo
好的,如果其中有任何信息,我想将这些列添加到一起。所以说我有 账户 1 2 3 . 有 4 个帐户空间,但只有 3 个帐户。我如何创建 java 脚本来添加它。 最佳答案 Live Example H
我想知道是否有一种有效的预制算法来确定一组数字的和/差是否可以等于不同的数字。示例: 5、8、10、2,使用 + 或 - 等于 9。5 - 8 = -3 + 10 = 7 + 2 = 9 如果有一个预
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我的 Oracle SQL 查询如下- Q1- select hca.account_number, hca.attribute3, SUM(rcl.extended_amou
我正在阅读 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
我正在尝试添加一个“加载更多”按钮并限制下面的结果,这样投资组合页面中就不会同时加载 1000 个内容,如下所示:http://typesetdesign.com/portfolio/ 我对 PHP
我遇到这个问题,我添加了 8 个文本框,它工作正常,但是当我添加更多文本框(如 16 个文本框)时,它不会添加最后一个文本框。有人遇到过这个问题吗?提前致谢。 Live Link: JAVASCRIP
add/remove clone first row default not delete 添加/删除克隆第一行默认不删除&并获取正确的SrNo(例如:添加3行并在看到问题后删除SrNo.2)
我编码this ,但删除按钮不起作用。我在控制台中没有任何错误.. var counter = 0; var dataList = document.getElementById('materi
我有一个类似数组的对象: [1:数组[10]、2:数组[2]、3:数组[2]、4:数组[2]、5:数组[3]、6:数组[1]] 我正在尝试删除前两个元素,执行一些操作,然后将它们再次插入到同一位置。
使用的 Delphi 版本:2007 你好, 我有一个 Tecord 数组 TInfo = Record Name : String; Price : Integer; end; var Info
我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件) 我的js代
title 323 323 323 title 323 323 323 title 323 323 323 JS $(document).keydown(function(e){
我是一名优秀的程序员,十分优秀!