Show Multiple data from SQLite database inside ListView in android one by one

How to set SQLite db multiple columns values into ListView using multiple rows in android application one by one.

ListView is most common view used to display text data into sequence inside android applications. Our in this tutorial first we are submitting ( Inserting ) values into SQLite database and after that on the next activity we are showing all the submitted data into ListView form. Our ListView format is different than other formats because we are showing multiple records in each listview row using custom XML file. So here is the complete step by step tutorial for Show Multiple data from SQLite database inside ListView in android.

android-project-download-code-button

List of Activity Java files in this tutorial :

  1. MainActivity.java file.
  2. ListViewActivity.java file.

List of custom java programming file which help us to set SQLite data into ListView :

  1. SQLiteHelper.java file.
  2. SQLiteListAdapter.java file.

List of all XML layout files :

  1. activity_main.xml file.
  2. activity_list_view.xml file.
  3. listviewdatalayout.xml file.

How to Show Multiple data from SQLite database inside ListView in android.

Code for MainActivity.java file.

 package com.showsqlitedbdatainsidelistview_android_examples.com;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

 EditText GetName,GetPhoneNumber,GetSubject ;
 Button Submit, ShowValues;
 SQLiteDatabase SQLITEDATABASE;
 String Name, PhoneNumber, Subject ;
 Boolean CheckEditTextEmpty ;
 String SQLiteQuery ;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 GetName = (EditText)findViewById(R.id.editText1);
 
 GetPhoneNumber = (EditText)findViewById(R.id.editText2);
 
 GetSubject = (EditText)findViewById(R.id.editText3);
 
 Submit = (Button)findViewById(R.id.button1);
 
 ShowValues = (Button)findViewById(R.id.button2);
 
 Submit.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 DBCreate();
 
 SubmitData2SQLiteDB();
 
 }
 });
 
 ShowValues.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 Intent intent = new Intent(MainActivity.this, ListViewActivity.class);
 startActivity(intent);
 
 }
 });
 }
 
 public void DBCreate(){
 
 SQLITEDATABASE = openOrCreateDatabase("DemoDataBase", Context.MODE_PRIVATE, null);
 
 SQLITEDATABASE.execSQL("CREATE TABLE IF NOT EXISTS demoTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, phone_number VARCHAR, subject VARCHAR);");
 }
 
 public void SubmitData2SQLiteDB(){
 
 Name = GetName.getText().toString();
 
 PhoneNumber = GetPhoneNumber.getText().toString();
 
 Subject = GetSubject.getText().toString();
 
 CheckEditTextIsEmptyOrNot( Name,PhoneNumber, Subject);
 
 if(CheckEditTextEmpty == true)
 {
 
 SQLiteQuery = "INSERT INTO demoTable (name,phone_number,subject) VALUES('"+Name+"', '"+PhoneNumber+"', '"+Subject+"');";
 
 SQLITEDATABASE.execSQL(SQLiteQuery);
 
 Toast.makeText(MainActivity.this,"Data Submit Successfully", Toast.LENGTH_LONG).show(); 
 
 ClearEditTextAfterDoneTask();
 
 }
 else {
 
 Toast.makeText(MainActivity.this,"Please Fill All the Fields", Toast.LENGTH_LONG).show();
 }
 }
 
 public void CheckEditTextIsEmptyOrNot(String Name,String PhoneNumber, String subject ){
 
 if(TextUtils.isEmpty(Name) || TextUtils.isEmpty(PhoneNumber) || TextUtils.isEmpty(Subject)){
 
 CheckEditTextEmpty = false ;
 
 }
 else {
 CheckEditTextEmpty = true ;
 }
 }
 
 public void ClearEditTextAfterDoneTask(){
 
 GetName.getText().clear();
 GetPhoneNumber.getText().clear();
 GetSubject.getText().clear();
 
 }
 
}

Code for ListViewActivity.java file.

 package com.showsqlitedbdatainsidelistview_android_examples.com;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewActivity extends Activity {
 
 SQLiteHelper SQLITEHELPER;
 SQLiteDatabase SQLITEDATABASE;
 Cursor cursor;
 SQLiteListAdapter ListAdapter ;

 ArrayList<String> ID_ArrayList = new ArrayList<String>();
 ArrayList<String> NAME_ArrayList = new ArrayList<String>();
 ArrayList<String> PHONE_NUMBER_ArrayList = new ArrayList<String>();
 ArrayList<String> SUBJECT_ArrayList = new ArrayList<String>();
 ListView LISTVIEW;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_list_view);

 LISTVIEW = (ListView) findViewById(R.id.listView1);

 SQLITEHELPER = new SQLiteHelper(this);
 
 }

 @Override
 protected void onResume() {

 ShowSQLiteDBdata() ;
 
 super.onResume();
 }

 private void ShowSQLiteDBdata() {
 
 SQLITEDATABASE = SQLITEHELPER.getWritableDatabase();
 
 cursor = SQLITEDATABASE.rawQuery("SELECT * FROM demoTable", null);

 ID_ArrayList.clear();
 NAME_ArrayList.clear();
 PHONE_NUMBER_ArrayList.clear();
 SUBJECT_ArrayList.clear();
 
 if (cursor.moveToFirst()) {
 do {
 ID_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_ID)));
 
 NAME_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_Name)));
 
 PHONE_NUMBER_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_PhoneNumber)));
 
 SUBJECT_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_Subject)));

 } while (cursor.moveToNext());
 }
 
 ListAdapter = new SQLiteListAdapter(ListViewActivity.this,
 
 ID_ArrayList,
 NAME_ArrayList,
 PHONE_NUMBER_ArrayList,
 SUBJECT_ArrayList 
 
 );
 
 LISTVIEW.setAdapter(ListAdapter);
 
 cursor.close();
 }
}

Code for SQLiteHelper.java file.

package com.showsqlitedbdatainsidelistview_android_examples.com;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteHelper extends SQLiteOpenHelper {
 
 static String DATABASE_NAME="DemoDataBase";
 
 public static final String KEY_ID="id";
 
 public static final String TABLE_NAME="demoTable";
 
 public static final String KEY_Name="name";
 
 public static final String KEY_PhoneNumber="phone_number";
 
 public static final String KEY_Subject="subject";
 
 public SQLiteHelper(Context context) {
 
 super(context, DATABASE_NAME, null, 1);
 
 }

 @Override
 public void onCreate(SQLiteDatabase database) {
 
 String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_Name+" VARCHAR, "+KEY_PhoneNumber+" VARCHAR, "+KEY_Subject+" VARCHAR)";
 database.execSQL(CREATE_TABLE);

 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
 onCreate(db);

 }

}

Code for SQLiteListAdapter.java file.

package com.showsqlitedbdatainsidelistview_android_examples.com;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SQLiteListAdapter extends BaseAdapter {
 
 Context context;
 ArrayList<String> userID;
 ArrayList<String> UserName;
 ArrayList<String> User_PhoneNumber;
 ArrayList<String> UserSubject ;
 

 public SQLiteListAdapter(
 Context context2,
 ArrayList<String> id,
 ArrayList<String> name,
 ArrayList<String> phone,
 ArrayList<String> subject
 ) 
 {
 
 this.context = context2;
 this.userID = id;
 this.UserName = name;
 this.User_PhoneNumber = phone;
 this.UserSubject = subject ;
 }

 public int getCount() {
 // TODO Auto-generated method stub
 return userID.size();
 }

 public Object getItem(int position) {
 // TODO Auto-generated method stub
 return null;
 }

 public long getItemId(int position) {
 // TODO Auto-generated method stub
 return 0;
 }

 public View getView(int position, View child, ViewGroup parent) {
 
 Holder holder;
 
 LayoutInflater layoutInflater;
 
 if (child == null) {
 layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 child = layoutInflater.inflate(R.layout.listviewdatalayout, null);
 
 holder = new Holder();
 
 holder.textviewid = (TextView) child.findViewById(R.id.textViewID);
 holder.textviewname = (TextView) child.findViewById(R.id.textViewNAME);
 holder.textviewphone_number = (TextView) child.findViewById(R.id.textViewPHONE_NUMBER);
 holder.textviewsubject = (TextView) child.findViewById(R.id.textViewSUBJECT);
 
 child.setTag(holder);
 
 } else {
 
 holder = (Holder) child.getTag();
 }
 holder.textviewid.setText(userID.get(position));
 holder.textviewname.setText(UserName.get(position));
 holder.textviewphone_number.setText(User_PhoneNumber.get(position));
 holder.textviewsubject.setText(UserSubject.get(position));

 return child;
 }

 public class Holder {
 TextView textviewid;
 TextView textviewname;
 TextView textviewphone_number;
 TextView textviewsubject;
 }

}

Code for activity_main.xml file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.showsqlitedbdatainsidelistview_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:ems="10"
 android:hint="Enter Name Here"
 android:gravity="center" />

 <EditText
 android:id="@+id/editText2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:ems="10"
 android:inputType="phone" 
 android:hint="Enter Phone Number Here"
 android:gravity="center" />

 <EditText
 android:id="@+id/editText3"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:ems="10" 
 android:hint="Enter Subject Here"
 android:gravity="center" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to Insert Data into SQLite Database" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="16dp"
 android:text="Click Here to See All data inside ListView" />
 
</RelativeLayout>

Code for activity_list_view.xml file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.showsqlitedbdatainsidelistview_android_examples.com.ListViewActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ListView>
 
</RelativeLayout>

Code for listviewdatalayout.xml file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true"
 android:text="ID = "
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

 <TextView
 android:id="@+id/textViewID"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBaseline="@+id/textView1"
 android:layout_alignBottom="@+id/textView1"
 android:layout_toRightOf="@+id/textView1"
 android:text="UserID"
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/textView1"
 android:text="Name = "
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

 <TextView
 android:id="@+id/textViewNAME"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textViewID"
 android:layout_toRightOf="@+id/textView2"
 android:text="Name"
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/textView2"
 android:text="Phone Number = "
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

 <TextView
 android:id="@+id/textViewPHONE_NUMBER"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBottom="@+id/textView3"
 android:layout_toRightOf="@+id/textView3"
 android:text="phone number"
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

 <TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/textView3"
 android:text="Subject = "
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

 <TextView
 android:id="@+id/textViewSUBJECT"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBaseline="@+id/textView4"
 android:layout_alignBottom="@+id/textView4"
 android:layout_toRightOf="@+id/textView4"
 android:text="subject"
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:textColor="#050505" />

</RelativeLayout>

Screenshots:

SQLITE-LISTVIEW-1

 

Show Multiple data from SQLite database inside ListView in android

Click here to download Show Multiple data from SQLite database inside ListView in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *