Tôi có chế độ xem danh sách với cursoradapter. Bây giờ tôi muốn triển khai quảng cáo gốc express trong chế độ xem danh sách.Cách triển khai Quảng cáo gốc express trong Chế độ xem danh sách với CursorAdapter
Tôi đã thấy việc triển khai Quảng cáo gốc với baseAdapter đơn giản, nói chung chúng tôi đang sử dụng List<Object>
để chuyển dữ liệu sang bộ điều hợp và kiểm tra loại mục bên trong phương thức getView()
để thêm quảng cáo.
@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()));
}
}
Làm thế nào để kiểm tra điều kiện này trong CursorAdapter như CursorAdapter chỉ có phương pháp newItem() với các chi tiết con trỏ
@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;
}
Làm thế nào để thêm quảng cáo có nguồn gốc sau mỗi 10 mục trong CursorAdapter
Bellow là mã hiện tại tôi đang sử dụng để thêm dữ liệu trong danh sách.
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;
}
}
Nhưng làm cách nào tôi có thể đặt viewType khi chúng tôi đang sử dụng bộ điều hợp curson? –
@OmInfowaveDevelopers Về cơ bản, bạn tạo một số ViewHolder. Mỗi để thực hiện của bạn. ViewHolderDog, ViewHolderCat. Trong 'NewView' bạn sẽ tìm nạp một số thông tin về kiểu từ Cursor, và sau đó trả về ViewHolder Type đặc biệt. Sau đó, trong 'OnBind', bạn sẽ nhận được ViewHolder, và bạn sẽ kiểm tra loại đó là gì. – GensaGames
, Nhưng vấn đề là CursorAdpater nó trực tiếp truyền con trỏ đến adapter, giả sử có 22 mục trong cơ sở dữ liệu cục bộ sau đó kích thước con trỏ là 22 thì kích thước bộ điều hợp cũng là 22, bây giờ tôi muốn thêm quảng cáo sau mỗi 10 mục để kích thước bộ điều hợp phải 24. Làm thế nào để thêm hai mục bổ sung này trong bộ điều hợp con trỏ vì chúng tôi không thể sửa đổi giá trị Con trỏ –