Note that there are some explanatory texts on larger screens.

plurals
  1. POViewPager Cursor position
    primarykey
    data
    text
    <p>I am writing a simple news app as a learning exercise. Display a listfragment driven by a SQLite cursor. Click and go to detail screen. Now trying to convert detail screen to ViewPager. I do not want to pass the Cursor from listfragment to ViewPager activity, so I pass the current position in the cursor instead and then create a new Cursor. Can't seem to get the position right though. If I set it in the Activity and not getItem() it goes to the correct position but never changes. If I set it in getItem, it always starts at position 0. The key lines are </p> <pre><code>// cp.getCursor().moveToPosition(mStartPosition); // cp.notifyDataSetChanged(); </code></pre> <p>Thanks for any help.</p> <pre><code> public class ViewPagerActivity extends FragmentActivity { private static final int NUM_PAGES = 5; private static String mSectionSelected = ""; private static int mStartPosition; private ViewPager mPager; private PagerAdapter mPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view_pager); // Instantiate a ViewPager and a PagerAdapter. mSectionSelected = (String)this.getIntent().getSerializableExtra("SECTION"); mPager = (ViewPager) this.findViewById(R.id.pager); CursorPagerAdapter cp = new CursorPagerAdapter( getSupportFragmentManager(), mSectionSelected); mPager.setAdapter(cp); // cp.getCursor().moveToPosition(mStartPosition); // cp.notifyDataSetChanged(); } @Override public void onBackPressed() { if (mPager.getCurrentItem() == 0) { // If the user is currently looking at the first step, allow the system to handle the // Back button. This calls finish() on this activity and pops the back stack. super.onBackPressed(); } else { // Otherwise, select the previous step. mPager.setCurrentItem(mPager.getCurrentItem() - 1); } } } </code></pre> <p>And here is the adapter - the key line is <code>cursor.moveToPosition(position);</code></p> <pre><code> public class CursorPagerAdapter extends FragmentStatePagerAdapter { private Cursor cursor; private static String mSectionSelected; public CursorPagerAdapter(FragmentManager fm, String mSection) { super(fm); mSectionSelected = mSection; this.cursor = NewsManager.get(ApplicationContext.getInstance()).queryNews(mSectionSelected); } @Override public PagerDetailFragment getItem(int position) { if (cursor == null) // shouldn't happen return null; cursor.moveToPosition(position); PagerDetailFragment frag; try { frag = PagerDetailFragment.newInstance(); String mTitle = cursor.getString(NewsDatabaseHelper.COLNUM_TITLE); String mSection = cursor.getString(NewsDatabaseHelper.COLNUM_CATEGORY); String mSource = cursor.getString(NewsDatabaseHelper.COLNUM_SOURCE); String mPublishedDate = cursor.getString(NewsDatabaseHelper.COLNUM_PUBLISHDATE); String mThumbnail = cursor.getString(NewsDatabaseHelper.COLNUM_THUMBNAIL); String mBody = cursor.getString(NewsDatabaseHelper.COLNUM_CONTENT); Bundle args = new Bundle(); args.putString(NewsDatabaseHelper.COLUMN_TITLE, mTitle); args.putString(NewsDatabaseHelper.COLUMN_CATEGORY, mSection); args.putString(NewsDatabaseHelper.COLUMN_SOURCE, mSource); args.putString(NewsDatabaseHelper.COLUMN_PUBLISHDATE, mPublishedDate); args.putString(NewsDatabaseHelper.COLUMN_THUMBNAIL, mThumbnail); args.putString(NewsDatabaseHelper.COLUMN_CONTENT, mBody); frag.setArguments(args); } catch (Exception ex) { throw new RuntimeException(ex); } return frag; } @Override public int getCount() { if (cursor == null) return 0; else return cursor.getCount(); } public void swapCursor(Cursor c) { if (cursor == c) return; this.cursor = c; notifyDataSetChanged(); } public Cursor getCursor() { return cursor; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload