Get Retrieve Multiple data from SQLite database in android and Display inside EditText

How to show SQLite Multiple data inside EditText one by one using Next Previous buttons.

In my previous tutorial we have talked about creating SQLite database and inserting values directly into database using EditText. Now in this tutorial we are going to do the same work but there is only one difference that we are going to display SQLite database data directly into EditText. So for my further coming tutorials we can edit that data directly form EditText. But for now in this tutorial we are only going to Get Retrieve Multiple data from SQLite database in android and Display inside EditText.

android-project-download-code-button

How to Get Retrieve Multiple data from SQLite database in android and Display inside EditText.

About This Tutorial :

  1. MainActivity.java file : Creating database + Tables, Inserting Data into SQLite db.
  2. GetDataActivity.java file : Showing SQLite records inside EditText with Next-Previous buttons .
  3. activity_main.xml file : Layout file for MainActivity.java file .
  4. activity_get_data.xml file : Layout file for GetDataActivity.java file .

Also read : How to insert data into SQLite Db using EditText tutorial.

Code for MainActivity.java file.

 package com.retrievedatafromsqlitedb_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, GetDataActivity.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 activity_main.xml layout 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.retrievedatafromsqlitedb_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 Enterted Values inside SQLite Database ONE By One" />
 
</RelativeLayout>

Code for GetDataActivity.java file.

 package com.retrievedatafromsqlitedb_android_examples.com;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
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.TextView;
import android.widget.Toast;

public class GetDataActivity extends Activity {

Button nextbutton, previousbutton ;
 
 EditText name, phoneNumber, SubJect;
 
 SQLiteDatabase SQLITEDATABASE ;
 
 String GetSQliteQuery, UpdateRecordQuery, DeleteQuery ;
 
 Cursor cursor ;
 
 TextView idtextview;
 
 Boolean CheckEditTextEmpty;
 
 String GetName,GetPhoneNumber,GetSubject ;
 
 int UserID ;
 
 String ConvertUserID ;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_get_data);
 
 nextbutton = (Button)findViewById(R.id.button1);
 previousbutton = (Button)findViewById(R.id.button2);
 
 name = (EditText)findViewById(R.id.editText1);
 phoneNumber = (EditText)findViewById(R.id.editText2);
 SubJect = (EditText)findViewById(R.id.editText3);
 
 idtextview = (TextView)findViewById(R.id.textview1);
 
 GetSQliteQuery = "SELECT * FROM demoTable" ;
 
 SQLITEDATABASE = openOrCreateDatabase("DemoDataBase", Context.MODE_PRIVATE, null);
 
 cursor = SQLITEDATABASE.rawQuery(GetSQliteQuery, null);
 
 cursor.moveToFirst();
 
 GetSQLiteDatabaseRecords();
 
 
 nextbutton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if (!cursor.isLast()){
 
 cursor.moveToNext();
 } 

 GetSQLiteDatabaseRecords();
 
 }
 });
 
 previousbutton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 if (!cursor.isFirst()){
 
 cursor.moveToPrevious();
 }
 GetSQLiteDatabaseRecords();

 }
 });
 }
 
 public void GetSQLiteDatabaseRecords(){

 idtextview.setText(cursor.getString(0));
 
 name.setText(cursor.getString(1).toString());
 
 phoneNumber.setText(cursor.getString(2).toString());
 
 SubJect.setText(cursor.getString(3).toString());
 }
 
 public void CheckEditTextIsEmptyOrNot(String Name,String PhoneNumber, String subject ){
 
 if(TextUtils.isEmpty(Name) || TextUtils.isEmpty(PhoneNumber) || TextUtils.isEmpty(subject)){
 
 CheckEditTextEmpty = true ;
 
 }
 else {
 CheckEditTextEmpty = false ;
 }
 }
 
}

Code for activity_get_data.xml layout 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.retrievedatafromsqlitedb_android_examples.com.GetDataActivity" >

 <TextView 
 android:id="@+id/textview1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:layout_centerHorizontal="true"
 android:text="Record Id = "
 android:textAppearance="?android:attr/textAppearanceLarge"/>
 
 <EditText
 android:id="@+id/editText1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textview1"
 android:layout_centerHorizontal="true"
 android:ems="10"
 android:hint="Name Show Here"
 android:gravity="center"
 android:layout_marginTop="20dp" />

 <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="Phone Number Show 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="Subject Show Here"
 android:gravity="center" />

 <Button
 android:id="@+id/button1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Show Next Record" />

 <Button
 android:id="@+id/button2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="Show Previous Record" />
 
</RelativeLayout>

Screenshots :

get-sqlite-data-2 Get Retrieve Multiple data from SQLite database in android and Display inside EditText get-sqlite-data-4

Click here to download Get Retrieve Multiple data from SQLite database in android and Display inside EditText project with source code.

Leave a Reply

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