Well I hava a listview and if clicked a listview element a Viewpager openning to show content.
both listview and Viewpager are same adaptor which is coming from database.
The problem is: when viewpager openning it needs to create same list adaptor to use. How can I use Listview's list for viewpager without creating again and again.
I take this codes from Big Nerd's book:
This is listfragment:
public class framelist extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
TestAdapter mDbHelper = new TestAdapter(getActivity());
mDbHelper.createDatabase();
mDbHelper.open();
List<element> mElements = mDbHelper.getTestData();
This is Viewpager FragmentActivity
public class CrimePagerActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
mElements = mDbHelper.getTestData();
And this is Viewpager fragment:
public class CrimeFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mNum2 = (int) getArguments().getInt("num");
TestAdapter mDbHelper = new TestAdapter(getActivity());
mDbHelper.createDatabase();
mDbHelper.open();
List<element> mElements = mDbHelper.getTestData();
melement = mElements.get(mNum2);
I suggest you use the
Loader
framework in order to query things. If nothing else, refer to my answer to similar question, it provides some very relevant links, that could help you in your case study.One more hint: if you just use straightforward approach, which is "query database through
OpenSqliteDbHelper
instance from yourActivity
", you have to be very careful and take care about your app lifecycle andCursor's
lifecycle management. And of course, it is very hard to improve performance, if you use such approach. All these problems disappear like magic if you use the suggestedLoader
approach, as it persist data on configuration change and you do not have to query your data again and again.One more hint: if you choose to use the straightforward (that is, without
Loader
) approach, make sure you query your data on a separate thread. That will keep your UI from being frozen.Similar questions were already asked and answered here, use search, if nothing else