1

我试图在使用resourcecursoradapter适配器的recyclerview中添加admob原生广告..问题是1 - 它使应用程序崩溃2 - 它用我的代码的admob广告替换recyclerview数据项

public class EntriesCursorAdapter extends ResourceCursorAdapter {

    private final Uri mUri;
    private final boolean mShowFeedInfo;
    private int mIdPos, mTitlePos, mMainImgPos, mDatePos, mIsReadPos, mFavoritePos, mFeedIdPos, mFeedNamePos;
    public static final int Ads_view_type = 11;

    public EntriesCursorAdapter(Context context, Uri uri, Cursor cursor, boolean showFeedInfo) {
        super(context, PrefUtils.getInt(PrefUtils.NEWS_FORMAT_int,R.layout.item_entry_list2), cursor, 0);
        mUri = uri;
        mShowFeedInfo = showFeedInfo;

        reinit(cursor);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final int position = cursor.getPosition();
        final int viewtype = getItemViewType(position);
        View itemView ;
        if(viewtype == Ads_view_type) {
            final LayoutInflater inflater = LayoutInflater.from(context);
            itemView = inflater.inflate(R.layout.native_ads, parent, false);
            if (itemView.getTag(R.id.holder) == null) {
                AdsHolder ads_holder = new AdsHolder();
                ads_holder.adView = (NativeExpressAdView) itemView.findViewById(R.id.adView2);
                itemView.setTag(R.id.holder, ads_holder);
            } return itemView;

        }
        else {
            final LayoutInflater inflater = LayoutInflater.from(context);
            itemView  = inflater.inflate(PrefUtils.getInt(PrefUtils.NEWS_FORMAT_int, R.layout.item_entry_list2), parent, false);
            if (itemView.getTag(R.id.holder) == null) {
                ViewHolder holder = new ViewHolder();
                holder.titleTextView = (TextView) itemView.findViewById(android.R.id.text1);
                holder.dateTextView = (TextView) itemView.findViewById(android.R.id.text2);
                holder.mainImgView = (ImageView) itemView.findViewById(R.id.main_icon);
                holder.starImgView = (ImageView) itemView.findViewById(R.id.favorite_icon);
                itemView.setTag(R.id.holder, holder);
            }
            return itemView;
           }
        }

    @Override
    public int getItemViewType(int position) {
        if(position % 6 ==0)
        {return Ads_view_type;}
        else {
            return super.getItemViewType(position);
        }

    }


    @Override
    public void bindView(View view, final Context context, Cursor cursor) {
        final int position = cursor.getPosition();
        final int viewtype = getItemViewType(position);
        if (viewtype == Ads_view_type) {
            try {
                AdsHolder adsHolder = (AdsHolder) view.getTag(R.id.holder);
                AdRequest request = new AdRequest.Builder()
                        .addTestDevice("ca-app-pub-5647351014779121/8591922893")
                        .build();
                adsHolder.adView.loadAd(request);
            } catch (Exception e) {
            }
        } else {
            try {
                ViewHolder holder = (ViewHolder) view.getTag(R.id.holder);

                String titleText = cursor.getString(mTitlePos);
                holder.titleTextView.setText(titleText);

                final long feedId = cursor.getLong(mFeedIdPos);
                String feedName = cursor.getString(mFeedNamePos);

                String mainImgUrl = cursor.getString(mMainImgPos);
                mainImgUrl = TextUtils.isEmpty(mainImgUrl) ? null : NetworkUtils.getDownloadedOrDistantImageUrl(cursor.getLong(mIdPos), mainImgUrl);

                ColorGenerator generator = ColorGenerator.DEFAULT;
                int color = generator.getColor(feedId); // The color is specific to the feedId (which shouldn't change)
                String lettersForName = feedName != null ? (feedName.length() < 2 ? feedName.toUpperCase() : feedName.substring(0, 2).toUpperCase()) : "";
                TextDrawable letterDrawable = TextDrawable.builder().buildRect(lettersForName, color);
                if (mainImgUrl != null) {
                    Glide.with(context).load(mainImgUrl).centerCrop().placeholder(letterDrawable).error(letterDrawable).into(holder.mainImgView);
                } else {
                    Glide.clear(holder.mainImgView);
                    holder.mainImgView.setImageDrawable(letterDrawable);
                }

                holder.isFavorite = cursor.getInt(mFavoritePos) == 1;

                holder.starImgView.setVisibility(holder.isFavorite ? View.VISIBLE : View.INVISIBLE);

                if (mShowFeedInfo && mFeedNamePos > -1) {
                    if (feedName != null) {
                        holder.dateTextView.setText(Html.fromHtml("<font color='#247ab0'>" + feedName + "</font>" + Constants.COMMA_SPACE + StringUtils.getDateTimeString(cursor.getLong(mDatePos))));
                    } else {
                        holder.dateTextView.setText(StringUtils.getDateTimeString(cursor.getLong(mDatePos)));
                    }
                } else {
                    holder.dateTextView.setText(StringUtils.getDateTimeString(cursor.getLong(mDatePos)));
                }

                if (cursor.isNull(mIsReadPos)) {
                    holder.titleTextView.setEnabled(true);
                    holder.dateTextView.setEnabled(true);
                    holder.isRead = false;
                } else {
                    holder.titleTextView.setEnabled(false);
                    holder.dateTextView.setEnabled(false);
                    holder.isRead = true;
                }
            } catch (Exception e) {}

        }
    }

    public void toggleReadState(final long id, View view) {
        final ViewHolder holder = (ViewHolder) view.getTag(R.id.holder);

        if (holder != null) { // should not happen, but I had a crash with this on PlayStore...
            holder.isRead = !holder.isRead;

            if (holder.isRead) {
                holder.titleTextView.setEnabled(false);
                holder.dateTextView.setEnabled(false);
            } else {
                holder.titleTextView.setEnabled(true);
                holder.dateTextView.setEnabled(true);
            }

            new Thread() {
                @Override
                public void run() {
                    ContentResolver cr = MainApplication.getContext().getContentResolver();
                    Uri entryUri = ContentUris.withAppendedId(mUri, id);
                    cr.update(entryUri, holder.isRead ? FeedData.getReadContentValues() : FeedData.getUnreadContentValues(), null, null);
                }
            }.start();
        }
    }

    public void toggleFavoriteState(final long id, View view) {
        final ViewHolder holder = (ViewHolder) view.getTag(R.id.holder);

        if (holder != null) { // should not happen, but I had a crash with this on PlayStore...
            holder.isFavorite = !holder.isFavorite;

            if (holder.isFavorite) {
                holder.starImgView.setVisibility(View.VISIBLE);
            } else {
                holder.starImgView.setVisibility(View.INVISIBLE);
            }

            new Thread() {
                @Override
                public void run() {
                    ContentValues values = new ContentValues();
                    values.put(EntryColumns.IS_FAVORITE, holder.isFavorite ? 1 : 0);

                    ContentResolver cr = MainApplication.getContext().getContentResolver();
                    Uri entryUri = ContentUris.withAppendedId(mUri, id);
                    cr.update(entryUri, values, null, null);
                }
            }.start();
        }
    }



    @Override
    public void changeCursor(Cursor cursor) {
        reinit(cursor);
        super.changeCursor(cursor);
    }

    @Override
    public Cursor swapCursor(Cursor newCursor) {
        reinit(newCursor);
        return super.swapCursor(newCursor);
    }

    @Override
    public void notifyDataSetChanged() {
        reinit(null);
        super.notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetInvalidated() {
        reinit(null);
        super.notifyDataSetInvalidated();
    }

    private void reinit(Cursor cursor) {
        if (cursor != null && cursor.getCount() > 0) {
            mIdPos = cursor.getColumnIndex(EntryColumns._ID);
            mTitlePos = cursor.getColumnIndex(EntryColumns.TITLE);
            mMainImgPos = cursor.getColumnIndex(EntryColumns.IMAGE_URL);
            mDatePos = cursor.getColumnIndex(EntryColumns.DATE);
            mIsReadPos = cursor.getColumnIndex(EntryColumns.IS_READ);
            mFavoritePos = cursor.getColumnIndex(EntryColumns.IS_FAVORITE);
            mFeedNamePos = cursor.getColumnIndex(FeedColumns.NAME);
            mFeedIdPos = cursor.getColumnIndex(EntryColumns.FEED_ID);
        }
    }

    private static class ViewHolder {
        public TextView titleTextView;
        public TextView dateTextView;
        public ImageView mainImgView;
        public ImageView starImgView;
        public boolean isRead, isFavorite;
    }
    private static class AdsHolder {
        NativeExpressAdView adView;
    }
}
4

0 回答 0