Search code examples
androidandroid-viewpagerandroid-sqliteandroid-cursoradapterfragmentpageradapter

How to load SQLite data into a viewpager?


I am new to android programming and I need to load each rows from an SQLite database into a viewpager (My SQLite database contains fields "ID, NAME, AGE, ADDRESS"), so that I can be able to navigate to each row by swiping left and right. I am unsuccessful in finding the right solution to this. I have been trying to solve this problem for a week now.

It would be a blessing if someone can help me out with this. Thank you very much!


Solution

  • After tons of research and tries, I was able to come up with a working solution. The code below is all listed in the MainActivity. Pretty straight forward. I hope this helps out other people out there. Leave a comment if you have any questions.

    public class MainActivity extends AppCompatActivity {
    
    float x1,x2;
    float y1, y2;
    
    int currentPage;
    ViewPager mViewPager;
    String ID;
    List<String> listID = new ArrayList<String>();
    List<String> listName = new ArrayList<String>();
    List<String> listMtrno = new ArrayList<String>();
    String SearchString = null;
    CursorPagerAdapter mAdapter;
    Cursor cursor;
    DatabaseHelper databaseHelper;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Bundle extras = getIntent().getExtras();
    
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    
        databaseHelper = new DatabaseHelper(this);
    
        setContentView(R.layout.pager);
    
        databaseHelper = new DatabaseHelper(MainActivity.this);
        cursor = databaseHelper.fetch();
        mAdapter = new CursorPagerAdapter(this, cursor);
    
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAdapter);
    
        mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
    
        if (extras != null)
        {
            SearchString = extras.getString("SearchFlag");
            if (SearchString != null) {
                mViewPager.setCurrentItem(Integer.parseInt(SearchString) - 1);
    
            } else {
                if (cursor.moveToFirst()){
                    while(!cursor.isAfterLast()){
                        cursor.moveToNext();
                    }
                }
            }
            Log.d("MainActivity", "Search(A): " + SearchString);
        }
    }
    
    private class CursorPagerAdapter extends PagerAdapter {
    
        private Cursor cursor;
        private LayoutInflater inflater;
    
        public CursorPagerAdapter(Context context, Cursor cursor) {
    
            Log.d("Main Activity", "MyCursorPagerAdapter.onCreate()");
    
            this.cursor = cursor;
            this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        public void swapCursor(Cursor cursor) {
            //if(cursor != null) {
    
            Log.d("Main Activity", "swapCursor().");
    
            this.cursor = cursor;
            //notifyDataSetChanged();
            //}
        }
        @Override
        public int getCount() {
            if(cursor == null) {
                return 0;
            } else {
                return cursor.getCount();
            }
        }
        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
    
            cursor.moveToPosition(position);
            (view).removeView((LinearLayout) object);
        }
        @Override
        public Object instantiateItem(ViewGroup view, int position) {
    
            position = position % cursor.getCount();
    
            cursor.moveToPosition(position);
    
            LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_listener, null);
    
            TextView f1_recno = layout.findViewById(R.id.TV_recno);
            TextView f1_acctno = layout.findViewById(R.id.TV_acctno);
            TextView f1_route = layout.findViewById(R.id.TV_route);
            TextView fl_name = layout.findViewById(R.id.TV_name);
            TextView fl_mtrno = layout.findViewById(R.id.TV_mtrno);
    
            f1_recno.setText((cursor.getPosition() + 1) + " of " + cursor.getCount() + " records");
            f1_acctno.setText(cursor.getString(cursor.getColumnIndex("ACCTNO")));
            f1_route.setText(cursor.getString(cursor.getColumnIndex("ROUTE")));
            fl_name.setText(cursor.getString(cursor.getColumnIndex("NAME")));
            fl_mtrno.setText(cursor.getString(cursor.getColumnIndex("MTRNO")));
    
                (view).addView(layout);
            return layout;
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
    

    I load a table from a database to a cursor object, and connect it to my CursorPagerAdapter. The loading of each row data is done in the InstantiateItem object.