3

I'm developing an app that shows the user a whole bunch of flash cards using a ViewPager. My problem is as follows - I have a custom PagerAdapter I made to handle cursors and get a whole bunch of data from my database and put it into a view. I want to add an option to delete a card but I'm not sure how to go about doing it. Here's my PagerAdapter code. Not too sure where to go from here.

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;
    }
}
nickmartens1980
  • 1,593
  • 13
  • 23
user2525981
  • 159
  • 1
  • 3
  • 10
  • Did you figure out how to delete the views from the viewpager? – Kala J Oct 15 '14 at 03:13
  • https://stackoverflow.com/a/62328370/3190214 Step by step on how to delete Data from Adaptor and how you let ViewPager know data is changed and should remove it – katwekibs Jun 11 '20 at 15:58

1 Answers1

3

For ViewPager deletes it really helps to understand the interaction of ViewPager with the adapter you implement. In particular, you should understand the data reference in the adapter ( usually Array ) and how the state of that object relates to calls of 'populate()' and 'notify()' over in the viewpager. VERY important is that on deletes, you need to account for following:

State of data array in the adapter ( hard delete or positional delete where set to null )

Call 'notifyDataChanged()' on the adapter which delegates to a number of calls on the VP

in mid of delegated calls on VP , in 'dataSetChanged()' , mAdapter.getItemPosition(ii.object) MUST return PagerAdapter.POSITION_NONE for deletes

see source

Note: some adapters used in ViewPager sample code return a default value on calls to 'getItemPostion()' and the default behavior does NOT work with deletes.

Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43