我有带光标适配器的列表视图。现在我想在 listview 中实现原生快递广告。
我已经看到使用简单的 baseAdapter 实现原生广告,因为通常我们List<Object>
用于将数据传递给适配器并检查getView()
方法内的项目类型以添加广告。
@Override
public View getView(int position, View convertView, ViewGroup parent)
throws IllegalArgumentException {
Object item = getItem(position);
if (item instanceof Listing) {
// Listing items already have all the data required, so they just need to be displayed.
return listingLayout;
} else if (item instanceof AdPlacement) {
return ((AdPlacement) item).getView(convertView, parent);
} else {
// Any unknown items will cause exceptions, though this shouldn't ever happen.
throw new IllegalArgumentException(
String.format("Adapter can't handle getView() for list item of type %s",
item.getClass().getName()));
}
}
如何在 cursoradapter 中检查此条件,因为 cursoradapter 只有具有光标详细信息的方法 newItem()
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.list_item_station, parent, false);
ViewHolder holder = new ViewHolder();
v.setTag(holder);
return v;
}
如何在 cursoradapter 中每 10 个项目后添加原生广告
Bellow 是我用来在列表中添加数据的当前代码。
public class StationsCursorAdapter extends CursorAdapter{
public StationsCursorAdapter(Context context) {
super(context, null, true);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.list_item_station, parent, false);
ViewHolder holder = new ViewHolder();
v.setTag(holder);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.titleTextView.setText(cursor.getString(cursor.getColumnIndex(Station.NAME)));
}
private static final class ViewHolder {
TextView titleTextView;
}
}