This is an android swipe tabs with ListView containing SQLite data.
In some of our tutorials we like combining several fundamental concepts so as to give a framework that can be used to build a real world app.
ViewPager – to achieve fragment swipeability.
TabLayout – To achieve tabbed navigation.
Fragments – To render our pages.
SQLite – To store our data in the local device.
These are important since it can help you build a full application.
In the small app that we are going to build, we will have three fragments. All these fragments will be hosted by the same activity.
Each fragment will hold a ListView with each ListView containing some data.
These fragments will be swipeable so users can use swiping which is a popular and modern navigation mechanism.
Also we will be able to use tabs to navigate our application.
The data inside our ListView will be saved in SQLite database. SQLite is a local file-based database written in C programming language and normally comes packaged already with android framework.
Of course before retrieving data from the SQLite database we will first need to insert that data in the first place. So we will be able to save data to SQLite via SQL INSERT statement, retrieve that data via SQL SELECT statement and bind the data in our ListViews.
Each ListView in each Fragment will basically comprise of a unique category of data. So we are categorizing our data in categories. Each category is stored in its fragment.
Creating a Movie Listing app, with each movie listed according to it’s own genre.
Creating an Ecommerce app with each product listed in its own tab according to product category.
Creating a Medical app with each disease showing prevention/treatment mechanism in a List.
Creating a News RSS app with each category listed in its own fragment with a List of News headlines.
It’s important to view th project structure to see how application is organized and which classes and layouts we create and use.
Below is one for this project.
Fire up your android studio and create an android project.
Android Studio is the defacto android development IDE supported by both Google and Jetbrains company.
Android Studio as you may know uses the gradle build system to build android projects.
So it generates a build.gradle in the app folder and we will modify the dependencies required by our project.
You can use the later versions of those dependencies.
We’ll now come and explore java classes. Android is just a mobile development framework and operating system and the real programming language used to write android apps is java, a very powerful programming language.
1. Spacecrafts.java Is our model class. Defines us one spacecraft’s properties.
2. InterStellar.java Our Inter-stellar spacecrafts fragment. Spacecrafts here will travel from one star to another within our galaxy.
3. InterPlanetary.java Our inter-planetary spacecrafts fragment. Spacececrafts here will travel from one planet to the other within our solar system.
4. InterGalactic.java Our inter-galactic spacecrafts fragment. The spacecrafts listed here will travel from one galaxy to another within the universe.
5. MyPagerAdapter.java Our FragmentPagerAdapter class. Will hold our fragments to be used for paging fragments by the ViewPager.
6. Constants.java Our sqlite database constants class. Will hold our database constants.
7. DBHelper.java Our sqlite database helper class. Will create and upgrade our database tables.
8. DBAdapter.java Our SQLite database adapter class. We will perform our SQLite CRUD right here.
9. MainActivity.java Our main and launcher activity.Will be responsible for hosting our fragments as well as our input dialog.
Let’s look at these classes one by one in great detail.
This class will represent for us a single spacecraft.
Name – Name of our spacecraft.
Category – Category of our spacecraft.
First create a java file in your android studio.
We will then create a public class. Public is an accessibility modifer used to denote that a class will be visible to other classes in other packages.
This is our fragment class. We’ll have two other fragment classes.
Fragments are subactivities introduced in Android 3.0 to allow for easy composition of modular UI units.
This class will show our inter-planetary spacecrafts in a ListView. This means that we need to define that ListView in this fragment and bind data to it from SQLite right here.
Fragments are subactivities hence deal with User Interface. They therefore have the capability to be inflated from XML Layouts.
To turn into a Fragment we have to derive from Fragment class.
android.support.v4.app.Fragment – Supports Library Fragment.
We’ll use the later since it allows us support devices prior to Android 3.0 when the fragment was introduced. This is unlike the first Fragment.
So with that InterPlanetary class is already a Fragment. An empty one.
So we are going to give it some content.
We define a ListView object, Button object and ArrayAdapter as instance fields.
First you can see we reference the ListView and refresh button. Then set the button onclick’s method and invoking the loadData() method.
You can see first we inflate the R.layout.interplanetary xml layout and returning it as our root view for the fragment. We return it after initializing our views via the initializeViews() method and loaded data from SQlite database via loadData() method.
This is our inter-setllar fragment.
It will be inflated from R.layout.interstellar layout. It will contain a ListView with inter-stellar spacecrafts from our SQLite database.
And create the class making it derive from android.support.v4.app.Fragment class.
Then define a method to load us data from SQLite database via the DBAdapter class that we will define later and bind the data to ListView.
We will override the toString() method which is always available to all Objects.
Our inter-galactic fragment. This Fragment will contain our inter-glactic spacecrafts rendered in a ListView.
This is our Database constants. This class will hold all our SQLite database constants.
Our SQLite Database Helper class.
This class will be be responsible for mainly creating our SQLite database table and upgrading it.
This class will derive from SQLiteOpenHelper clas defined in the android.database.sqlite. package.
We’ll define our constructor that takes in a Context object.
Inside the onCreate() as you can see we are creating our database table by invoking the execSQL() method of our SQLiteDatabase class.
Two integers : Old table version and new table version.
We’ll define a public method that will attempt to save data to SQLite database returning a boolean based on the success of this operation.
We’ll also define a method to retrieve data from SQLite database and populate and return an arraylist.
In the above two method we are catching SQLException.
This class will derive from FragmentPagerAdapter residing in the android.support.v4.app package.
This class wil hold our fragments that will be paged via the ViewPager.
This is our MainActivity. Activity is an android component representing a screen with which the user can interact with.
Host our Fragments, ViewPager and TabLayout.
We’ll also override several TabLayout.OnTabSelectedListener interface methods. We set the current item to our ViewPager inside the onTabSelected() method.
Reference our ViewPager from our xml layout.
Add Fragments to our MyPagerAdapter.
Set Tab Gravity to TabLayout.GRAVITY_FILL.
Setup our Tab with ViewPager.
Set Tab Listener to this Activity.