Question

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);
Was it helpful?

Solution

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 your Activity", you have to be very careful and take care about your app lifecycle and Cursor'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 suggested Loader 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

OTHER TIPS

What are you trying to achieve by showing the same content in both list as well as viewpager

Anyway, in your CrimePagerActivity instead of recreating a list again, why not copy the contents to the list

List<element> newList = mDbHelper.getTestData();
mElements.clear();
mElements..addAll(newList);

One more advice, your subject says increasing db performance , but you are asking all about list

Follow singleton pattern for DB and donot create new DB everytime, and make sure you synchronize any calls to db

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top