Course Content

Course Content

Description

How To Set Up Address Book On Android Phone

Till now we have seen many concept like in Database we can write data using Internal storage, Shared preferences etc... Now we will see that how we can apply SQLite practically.

First of all we will create a project named 'AddressBook' in our Android studio. And after that we will open its 'AndroidManifest.xml' file which is default generated, we have just added an icon, previously we used to have default android icon but now we have stored an icon in drawable folder so that is why we are going to display our own application icon in this case.

Now we will open our 'main.xml' file which is inside our layout folder to decide the designing of our application. Designing for this application is quite lengthy, but easy to understand.

So first of all we have taken an absolute layout here, and created an 'Exit' button with its usual properties. Now we will use a new concept 'ViewFlipper' here, in which we will use four different TableLayout. To change to different view from one to another we will use the concept of viewflipper for our application.

Now we will design our tag for viewflipper in our 'main.xml' file,

<ViewFlipper

android:id=""@+id/details""

android:layout_width=""fill_parent""

android:layout_height=""fill_parent""

/>

Since we have designed ViewFlipper's basic properties, we will create TableLayout, in which we will use one 'ListView' and two 'Buttons' to add or delete any content.

Now in our second TableLayout, we have used 'TextView', to display the details of what user selected in the first activity.

After that in our third TableLayout, we need to designe a 'delete' button, sofa that we will add content here. And in this layout we will also have back button.

Then in next TableLayout we have simply used buttons, Button 'back' and Button 'save'.

Now we will design it's code which is inside the 'java' folder, here we have two classes, one main activitiy in which are main code will be stored, and other one is 'DataHelper' in which we will generate code to create database.

So first we will open our DataHelper class in which we will see that we have declared some kind of strings,

private static final String DATABASE_NAME=""AddressBook.db"";

private static final int DATABASE_VERSION=1;

private static final String TABLE_NAME=""AddressContact"";

private Context context;

private SQLiteDatabase db;

private SQLiteStatement insertStat;

private static final String INSERT=""insert into""+TABLE_NAME+""(name.address)value (?,?)"";

 

Now we will see that how we can create database using DataHelper,

 

private static class OpenHelper extends SQLiteOpenHelper

{

OpenHelper (Context context)

{

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate (SQLiteDatabase db)

{ db.execSQL(""CREATE TABLE"",TABLE_NAME,""(id INTEGER PRIMARY KEY, name TEXT, address TEXT)"");

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

{

Log.v(""Address"",""Upgrading database, this will drop tables and recreate."");

db.execSQL(""DROP TABLE IF EXISTS""+TABLE_NAME);

onCreate(db);

}

 

Now we will design the constructor of class DataHelper,

 

public DataHelper(Context context)

{

this.context=context;

OpenHelper.openHelper=new OpenHelper(this.context);

this.db=openHelper.getWritableDatabase();

this.insertStat=this.db.compileStatement(INSERT);

}

 

To insert the content using a method,

 

public long insert (String name, String address)

{

try

{

this.insertStat.bindString(1, name);

this.insertStat.bindString(2, address);

return this.InsertStat.executeInsert();

}

catch(SQLException ex)

{

return 0;

}

}

 

To delete data from database name and address,

 

public void delete (String id) {

this.db.delete(TABLE_NAME,""id=""+id"",null);

}

public void deleteAll()

{

this.db.delete(TABLE_NAME,null, null);

}

 

Any content which is getting added or deleted should have a connection with the list which should be in database, so to establish that connection,

 

public List<String> selectAll()

{

List<String> list=new ArrayList<>();

Cursor cursor=this.db.query(TABLE_NAME, new String[] {""name""}, null, null, null, null, ""name desc"");

if {cursor.saveToFirst()}

{

do

{

list.add(cursor.getString{0}};

}

While (cursor.moveToNext());

}

if {cursor != null && !cursor.isClosed()

}

cursor.close();

}

return list;

}

 

To select a specific content we can generate code with help of 'cursor',

 

Cursor cursor=this.db.query{TABLE_NAME, new String[]

{

""id"",""name"",""address""

}

""name='""+name+"",null, null, null, null

};

 

Now we will look in the main activity file, the first thing which we we will do is to declare all the other contents which we used, so,

 

private com.android.addressBookDatahandling.DataHelper dh;

private ViewFlipper vf;

ListView listview=null;

 

Now in in 'onCreate' we have done basic things like view identification for edittext, viewflipper, list view, button OnClickListener.

 

Now to flip between different views we have used viewflipper in following way,

public void onClick (View V)

{

vf.showNext();

vf.showNext();

textboxName.setText("""");

textboxAddress.setText("""");

}

 

After that we have done identification of our exit button, which will simply finish the activity.

Now we will see that how 'Save' button works, so for that, first we will look that is both of our text boxes filled with content, if not then we will use alert box to display 'All fields are mandatory'. When text is passed, it will show notification that 'New contact saved successfully'.

Now in last we have used fillListView(), in which with the help of Adapter we have fill the content.

Now when we execute our code, it runs successfully as we needed.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere