0

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);
mehmet
  • 1,558
  • 5
  • 30
  • 41

2 Answers2

0

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

BlackBeard
  • 145
  • 6
  • For better sqlite performance take a look at [batch processing](http://gafurbabu.wordpress.com/2012/04/23/batch-insert-to-sqlite-database-on-android/) on sqlite – BlackBeard Mar 17 '14 at 08:35
  • Of course I am using singleton, problem is how can I use has already created list from DB in listview and Viewpager. but your approach can be improved, copying list? – mehmet Mar 17 '14 at 08:56
  • couldn't you pass the same list reference to the list as well as fragment – BlackBeard Mar 17 '14 at 09:00
  • not yet, everytime my list return empty if I try to copy, So How can call ListFragment List which is created with List mElements = mDbHelper.getTestData(); from viewpager without creat again – mehmet Mar 17 '14 at 09:49
  • if you are still using the same code then you are probably creating new db in every fragment, so ensure there is some data before querying the db. You can get the db to your desktop and verify if data is present – BlackBeard Mar 17 '14 at 09:55
0

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

Community
  • 1
  • 1
Drew
  • 3,307
  • 22
  • 33