Android SQLite ListView With Examples
Android SQLite ListView With Examples
SQLite is an open source light weight relational database management system (RDBMS) to perform
database operations, such as storing, updating, retrieving data from database. To know more about
SQLite, check this SQLite Tutorial with Examples.
Generally, in our android applications Shared Preferences, Internal Storage and External Storage options
are useful to store and maintain the small amount of data. In case, if we want to deal with large amount of
data, then SQLite database is the preferable option to store and maintain the data in structured format.
To know more about using SQLite Database in android applications, check this Android SQLite Database
Tutorial with Examples.
Now we will see how to create & insert data into SQLite Database and how to retrieve and show the data
in custom listview in android application with examples.
Following is the example of creating the SQLite database, insert and show the details from SQLite
database into android listview using SQLiteOpenHelper class.
Create a new android application using android studio and give names as SQLiteExample. In case if you
are not aware of creating an app in android studio check this article Android Hello World App.
Once we create a new class file DbHandler.java, open it and write the code like as shown below
DbHandler.java
package com.tutlane.sqliteexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
/**
*/
@Override
db.execSQL(CREATE_TABLE);
@Override
onCreate(db);
SQLiteDatabase db = this.getWritableDatabase();
//Create a new map of values, where column names are the keys
ContentValues cValues = new ContentValues();
cValues.put(KEY_NAME, name);
cValues.put(KEY_LOC, location);
cValues.put(KEY_DESG, designation);
// Insert the new row, returning the primary key value of the new row
db.close();
SQLiteDatabase db = this.getWritableDatabase();
while (cursor.moveToNext()){
user.put("name",cursor.getString(cursor.getColumnIndex(KEY_NAME)));
user.put("designation",cursor.getString(cursor.getColumnIndex(KEY_DESG)));
user.put("location",cursor.getString(cursor.getColumnIndex(KEY_LOC)));
userList.add(user);
return userList;
}
// Get User Details based on userid
SQLiteDatabase db = this.getWritableDatabase();
if (cursor.moveToNext()){
user.put("name",cursor.getString(cursor.getColumnIndex(KEY_NAME)));
user.put("designation",cursor.getString(cursor.getColumnIndex(KEY_DESG)));
user.put("location",cursor.getString(cursor.getColumnIndex(KEY_LOC)));
userList.add(user);
return userList;
SQLiteDatabase db = this.getWritableDatabase();
db.close();
}
// Update User Details
SQLiteDatabase db = this.getWritableDatabase();
cVals.put(KEY_LOC, location);
cVals.put(KEY_DESG, designation);
return count;
If you observe above code, we implemented all SQLite Database related activities to perform CRUD
operations in android application.
Now open activity_main.xml file from \res\layout folder path and write the code like as shown below.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="Name" />
<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<TextView
android:id="@+id/thirdTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Designation"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtDesignation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Save" />
</LinearLayout>
Now we will create another layout resource file details.xml in \res\layout path to show the details in
custom listview from SQLite Database for that right click on your layout folder à Go to New à select
Layout Resource File and give name as details.xml.
Once we create a new layout resource file details.xml, open it and write the code like as shown below
details.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/user_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Back" />
</LinearLayout>
Create an another layout file (list_row.xml) in /res/layout folder to show the data in listview, for that right
click on layout folder à add new Layout resource file à Give name as list_row.xml and write the code
like as shown below.
list_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="17dp" />
<TextView
android:id="@+id/designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="7dp"
android:textColor="#343434"
android:textSize="14dp" />
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/designation"
android:layout_alignBottom="@+id/designation"
android:layout_alignParentRight="true"
android:textColor="#343434"
android:textSize="14dp" />
</RelativeLayout>
Now open your main activity file MainActivity.java from \java\com.tutlane.sqliteexample path and write
the code like as shown below
MainActivity.java
package com.tutlane.sqliteexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Button saveBtn;
Intent intent;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.txtName);
loc = (EditText)findViewById(R.id.txtLocation);
desig = (EditText)findViewById(R.id.txtDesignation);
saveBtn = (Button)findViewById(R.id.btnSave);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
dbHandler.insertUserDetails(username,location,designation);
startActivity(intent);
});
If you observe above code, we are taking entered user details and inserting into SQLite database and
redirecting the user to another activity.
Once we create a new activity file DetailsActivity.java, open it and write the code like as shown below
DetailsActivity.java
package com.tutlane.sqliteexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
/**
*/
Intent intent;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
DbHandler db = new DbHandler(this);
lv.setAdapter(adapter);
back.setOnClickListener(new View.OnClickListener() {
@Override
startActivity(intent);
});
If you observe above code, we are getting the details from SQLite database and binding the details to
android listview. Now we need to add this newly created activity in AndroidManifest.xml file in like as
shown below.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.sqliteexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
If you observe above example, we are saving entered details in SQLite database and redirecting the user
to another activity file (DetailsActivity.java) to show the users details and added all the activities in
AndroidManifest.xml file.
Output of Android SQLite ListView Example
When we run above example in android emulator we will get a result like as shown below.
If you observe above result, the entered user details are storing in SQLite database and redirecting the
user to another activity file to show the user details from SQLite Database in listview. After that, if we click
on Back button, it will redirect user to login page.
This is how we can get the data from SQLite database and bind it to custom listview in android
applications based on our requirements.
Reference
1. https://www.tutlane.com/tutorial/android/android-sqlite-listview-with-examples