我正在开发一个应用程序,使用ViewPager向用户展示一堆闪卡。我的问题如下——我有一个自定义的PagerAdapter,用于处理游标并从我的数据库获取大量数据并将其放入视图中。我想添加一个选项来删除一张卡片,但我不确定如何做。这是我的PagerAdapter代码。不太确定接下来该怎么做。
public class CardCursorPagerAdapter extends PagerAdapter {
private Cursor cursor;
private LayoutInflater inflater;
public CardCursorPagerAdapter(Context context, Cursor cursor){
this.cursor = cursor;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void swapCursor(Cursor cursor){
this.cursor = cursor;
}
@Override
public void destroyItem(View view, int position, Object object) {
((ViewPager) view).removeView((LinearLayout) object);
}
@Override
public int getCount() {
if (cursor == null) {
return 0;
} else {
return 1000;
}
}
@Override
public Object instantiateItem(View view, int position) {
LinearLayout layout;
int position2 = position;
if (position == 0){
layout = (LinearLayout) inflater.inflate(R.layout.activity_slide_info, null);
} else {
position2++;
position2 = position2 % cursor.getCount();
cursor.moveToPosition(position2);
layout = (LinearLayout) inflater.inflate(R.layout.activity_card, null);
TextView cardTitle = (TextView) layout.findViewById(R.id.card_title);
TextView cardExample = (TextView) layout.findViewById(R.id.card_example);
TextView cardDefinition = (TextView) layout.findViewById(R.id.card_definition);
cardTitle.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_TITLE)));
cardExample.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_SENTENCE)));
cardDefinition.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_DEFINITION)));
}
((ViewPager) view).addView(layout);
return layout;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}