In our previous tutorial we have discussed about Inserting Data into SQLite database that tutorial is the first part of a series of tutorials about SQLite Database. This tutorial is the second part of SQLite database because in this tutorial we would going to Display Multiple SQLite Database Data into ListView at activity run time. We would show all the SQLite DB data into a ListView and each ListView item(Row) contains different-different rows like in this example there are three rows ID, Name and Phone Number within a single ListView Item.
Things we have done in this project :
- Creating SQLite Database.
- Creating Table in SQLite Database.
- Inserting data into SQLite table using EditText.
- Show all the inserted data into ListView one by one.
Project Details :
List of Activities in this project :
- MainActivity.java .
- DisplaySQLiteDataActivity.java .
List of Java Files in this project :
- SQLiteHelper.java .
- ListAdapter.java .
List of layout files in this project :
- activity_main.xml .
- activity_display_sqlite_data.xml .
- items.xml .
Watch Live Demo Explained How this App Works Display Multiple SQLite Database Data :
The demo video would explain you all about this tutorial like how it works and it also supports Android Marshmallow 6. Next step is start coding.
Coding Starts form Here to Display Multiple SQLite Database Data into ListView :
Code for MainActivity.java file.
package com.androidjson.sqlitepart_2_androidjsoncom; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; 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 AppCompatActivity { SQLiteDatabase sqLiteDatabaseObj; EditText editTextName, editTextPhoneNumber; String NameHolder, NumberHolder, SQLiteDataBaseQueryHolder; Button EnterData, ButtonDisplayData; Boolean EditTextEmptyHold; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EnterData = (Button)findViewById(R.id.button); ButtonDisplayData = (Button)findViewById(R.id.button2); editTextName = (EditText)findViewById(R.id.editText); editTextPhoneNumber = (EditText)findViewById(R.id.editText2); EnterData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SQLiteDataBaseBuild(); SQLiteTableBuild(); CheckEditTextStatus(); InsertDataIntoSQLiteDatabase(); EmptyEditTextAfterDataInsert(); } }); ButtonDisplayData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, DisplaySQLiteDataActivity.class); startActivity(intent); } }); } public void SQLiteDataBaseBuild(){ sqLiteDatabaseObj = openOrCreateDatabase(SQLiteHelper.DATABASE_NAME, Context.MODE_PRIVATE, null); } public void SQLiteTableBuild(){ sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS "+SQLiteHelper.TABLE_NAME+"("+SQLiteHelper.Table_Column_ID+" PRIMARY KEY AUTOINCREMENT NOT NULL, "+SQLiteHelper.Table_Column_1_Name+" VARCHAR, "+SQLiteHelper.Table_Column_2_PhoneNumber+" VARCHAR);"); } public void CheckEditTextStatus(){ NameHolder = editTextName.getText().toString() ; NumberHolder = editTextPhoneNumber.getText().toString(); if(TextUtils.isEmpty(NameHolder) || TextUtils.isEmpty(NumberHolder)){ EditTextEmptyHold = false ; } else { EditTextEmptyHold = true ; } } public void InsertDataIntoSQLiteDatabase(){ if(EditTextEmptyHold == true) { SQLiteDataBaseQueryHolder = "INSERT INTO "+SQLiteHelper.TABLE_NAME+" (name,phone_number) VALUES('"+NameHolder+"', '"+NumberHolder+"');"; sqLiteDatabaseObj.execSQL(SQLiteDataBaseQueryHolder); Toast.makeText(MainActivity.this,"Data Inserted Successfully", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"Please Fill All The Required Fields.", Toast.LENGTH_LONG).show(); } } public void EmptyEditTextAfterDataInsert(){ editTextName.getText().clear(); editTextPhoneNumber.getText().clear(); } }
Code for activity_main.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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.androidjson.sqlitepart_2_androidjsoncom.MainActivity"> <TextView android:text="SQLite Insert Into Database" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/textView" android:gravity="center" android:textSize="20dp" android:textColor="#000000"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:hint="Enter Name" android:ems="10" android:layout_marginTop="27dp" android:id="@+id/editText" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:gravity="center"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="Enter Phone Number" android:gravity="center" android:ems="10" android:layout_below="@+id/editText" android:layout_centerHorizontal="true" android:layout_marginTop="37dp" android:id="@+id/editText2" /> <Button android:text="Insert Into SQLite Database" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" android:id="@+id/button" /> <Button android:text="Display SQLite Database Data into ListView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button" android:layout_centerHorizontal="true" android:layout_marginTop="11dp" android:id="@+id/button2" /> </RelativeLayout>
Code for DisplaySQLiteDataActivity.java file.
package com.androidjson.sqlitepart_2_androidjsoncom; import android.support.v7.app.AppCompatActivity; import java.util.ArrayList; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.ListView; public class DisplaySQLiteDataActivity extends AppCompatActivity { SQLiteHelper sqLiteHelper; SQLiteDatabase sqLiteDatabase; Cursor cursor; ListAdapter listAdapter ; ListView LISTVIEW; ArrayList<String> ID_Array; ArrayList<String> NAME_Array; ArrayList<String> PHONE_NUMBER_Array; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_sqlite_data); LISTVIEW = (ListView) findViewById(R.id.listView1); ID_Array = new ArrayList<String>(); NAME_Array = new ArrayList<String>(); PHONE_NUMBER_Array = new ArrayList<String>(); sqLiteHelper = new SQLiteHelper(this); } @Override protected void onResume() { ShowSQLiteDBdata() ; super.onResume(); } private void ShowSQLiteDBdata() { sqLiteDatabase = sqLiteHelper.getWritableDatabase(); cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+SQLiteHelper.TABLE_NAME+"", null); ID_Array.clear(); NAME_Array.clear(); PHONE_NUMBER_Array.clear(); if (cursor.moveToFirst()) { do { ID_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_ID))); NAME_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_1_Name))); PHONE_NUMBER_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_2_PhoneNumber))); } while (cursor.moveToNext()); } listAdapter = new ListAdapter(DisplaySQLiteDataActivity.this, ID_Array, NAME_Array, PHONE_NUMBER_Array ); LISTVIEW.setAdapter(listAdapter); cursor.close(); } }
Code for activity_display_sqlite_data.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_display_sqlite_data" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" tools:context="com.androidjson.sqlitepart_2_androidjsoncom.DisplaySQLiteDataActivity"> <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 ListAdapter.java file.
package com.androidjson.sqlitepart_2_androidjsoncom; /** * Created by Juned on 1/23/2017. */ 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 ListAdapter extends BaseAdapter { Context context; ArrayList<String> ID; ArrayList<String> Name; ArrayList<String> PhoneNumber; public ListAdapter( Context context2, ArrayList<String> id, ArrayList<String> name, ArrayList<String> phone ) { this.context = context2; this.ID = id; this.Name = name; this.PhoneNumber = phone; } public int getCount() { // TODO Auto-generated method stub return ID.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.items, null); holder = new Holder(); holder.ID_TextView = (TextView) child.findViewById(R.id.textViewID); holder.Name_TextView = (TextView) child.findViewById(R.id.textViewNAME); holder.PhoneNumberTextView = (TextView) child.findViewById(R.id.textViewPHONE_NUMBER); child.setTag(holder); } else { holder = (Holder) child.getTag(); } holder.ID_TextView.setText(ID.get(position)); holder.Name_TextView.setText(Name.get(position)); holder.PhoneNumberTextView.setText(PhoneNumber.get(position)); return child; } public class Holder { TextView ID_TextView; TextView Name_TextView; TextView PhoneNumberTextView; } }
Code for SQLiteHelper.java layout file.
package com.androidjson.sqlitepart_2_androidjsoncom; /** * Created by Juned on 1/23/2017. */ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SQLiteHelper extends SQLiteOpenHelper { static String DATABASE_NAME="AndroidJSonDataBase"; public static final String TABLE_NAME="AndroidJSonTable"; public static final String Table_Column_ID="id"; public static final String Table_Column_1_Name="name"; public static final String Table_Column_2_PhoneNumber="phone_number"; public SQLiteHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase database) { String CREATE_TABLE="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("+Table_Column_ID+" INTEGER PRIMARY KEY, "+Table_Column_1_Name+" VARCHAR, "+Table_Column_2_PhoneNumber+" 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 items.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"> <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" /> </RelativeLayout>
Screenshots: