CursorPagerAdapter
-
-
Save JigneshWorld/c5d2215b0b4d25fbf0632aad37eac517 to your computer and use it in GitHub Desktop.
CursorPagerAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.meetup.adapter; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentStatePagerAdapter; | |
public class CursorPagerAdapter<F extends Fragment> extends FragmentStatePagerAdapter { | |
private final Class<F> fragmentClass; | |
private final String[] projection; | |
private Cursor cursor; | |
public CursorPagerAdapter(FragmentManager fm, Class<F> fragmentClass, String[] projection, Cursor cursor) { | |
super(fm); | |
this.fragmentClass = fragmentClass; | |
this.projection = projection; | |
this.cursor = cursor; | |
} | |
@Override | |
public F getItem(int position) { | |
if (cursor == null) // shouldn't happen | |
return null; | |
cursor.moveToPosition(position); | |
F frag; | |
try { | |
frag = fragmentClass.newInstance(); | |
} catch (Exception ex) { | |
throw new RuntimeException(ex); | |
} | |
Bundle args = new Bundle(); | |
for (int i = 0; i < projection.length; ++i) { | |
args.putString(projection[i], cursor.getString(i)); | |
} | |
frag.setArguments(args); | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment