Advertisement
Guest User

CursorPagerAdapter

a guest
Dec 11th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import android.database.Cursor;
  2. import android.support.v4.app.Fragment;
  3. import android.support.v4.app.FragmentManager;
  4. import android.support.v4.app.FragmentPagerAdapter;
  5.  
  6. public abstract class CursorPagerAdapter extends FragmentPagerAdapter {
  7.    
  8.     private Cursor cursor;
  9.  
  10.     public CursorPagerAdapter(FragmentManager fm) {
  11.         this(fm, null);
  12.     }
  13.    
  14.     public CursorPagerAdapter(FragmentManager fm, Cursor cursor) {
  15.         super(fm);
  16.         this.cursor = cursor;
  17.     }
  18.  
  19.     @Override
  20.     public Fragment getItem(int position) {
  21.         assert cursor != null;
  22.         cursor.moveToPosition(position);
  23.         return createFragment(cursor);
  24.     }
  25.  
  26.     protected abstract Fragment createFragment(Cursor cursor);
  27.    
  28.     @Override
  29.     public int getCount() {
  30.         return (cursor == null) ? 0 : cursor.getCount();
  31.     }
  32.  
  33.     public void swapCursor(Cursor c) {
  34.         if (cursor != c) {
  35.             this.cursor = c;
  36.             notifyDataSetChanged();
  37.         }
  38.     }
  39.  
  40.     public Cursor getCursor() {
  41.         return cursor;
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement