Wednesday, 25 May 2016

SQLLITE

06:32

SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c

Database - Package
The main package is android.database.sqlite that contains the classes to manage your own databases.

Simple example of   inserting  values from  form to  sqlite  database……

Database - Creation
In order to create a database you just need to call this method openOrCreateDatabase with your database name and mode as a parameter. It returns an instance of SQLite database which you have to receive in your own object.Its syntax is given below
SQLiteDatabase mydatabase = openOrCreateDatabase("your database name",MODE_PRIVATE,null);

Database - Insertion

we can create table or insert data into table using execSQL method defined in SQLiteDatabase class. Its syntax is given below
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS Tutorials(Username VARCHAR,Password VARCHAR);");
mydatabase.execSQL("INSERT INTO Tutorials  VALUES('admin','admin');");

execSQL(String sql, Object[] bindArgs)
This method not only insert data , but also used to update or modify already existing data in database using bind arguments

 Database - Fetching
We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data.
Cursor resultSet = mydatbase.rawQuery("Select * from Tutorials",null);
resultSet.moveToFirst();
String username = resultSet.getString(1);
String password = resultSet.getString(2);

 ===============================================================================================================================================


(1)write      code  for     MainActivity.java    file  :
package testing.om.com.testingdb; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements View.OnClickListener {     private EditText editTextName;     private EditText editTextAdd;     private Button btnAdd;     private Button btnView;     private SQLiteDatabase db;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         createDatabase();         editTextName = (EditText) findViewById(R.id.editTextName);         editTextAdd = (EditText) findViewById(R.id.editTextAddress);         btnAdd = (Button) findViewById(R.id.btnAdd);         btnView = (Button) findViewById(R.id.btnView);         btnAdd.setOnClickListener(this);         btnView.setOnClickListener(this);     }     protected void createDatabase(){         db=openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);  db.execSQL("CREATE TABLE IF NOT EXISTS persons(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");     } protected void insertIntoDB(){    String name = editTextName.getText().toString().trim();    String add = editTextAdd.getText().toString().trim();   if(name.equals("") || add.equals("")){ Toast.makeText(getApplicationContext(),"Please fill all fields", Toast.LENGTH_LONG).show();    return;         }  String query = "INSERT INTO persons (name,address) VALUES('"+name+"', '"+add+"');";    db.execSQL(query);   Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).show();     } protected void showDatabase() {   Cursor resultSet = db.rawQuery("Select * from persons", null);   resultSet.moveToFirst();   String name = resultSet.getString(1);   String  add = resultSet.getString(2);   Toast.makeText(getApplicationContext(),"name is "+name,Toast.LENGTH_SHORT).show();  Toast.makeText(getApplicationContext(),"name is "+add,Toast.LENGTH_SHORT).show(); }   @Override  public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.menu_main, menu);         return true;     }     @Override     public boolean onOptionsItemSelected(MenuItem item) {         // Handle action bar item clicks here. The action bar will         // automatically handle clicks on the Home/Up button, so long         // as you specify a parent activity in AndroidManifest.xml.         int id = item.getItemId();         //noinspection SimplifiableIfStatement         if (id == R.id.action_settings) {             return true;         }         return super.onOptionsItemSelected(item);     }     @Override     public void onClick(View v)     {         if(v == btnAdd) {             insertIntoDB();         }             if(v == btnView){                 showDatabase();             }     } }

(2) activity_main.xml      file    :
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:orientation="vertical"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Name"

        android:id="@+id/textViewName" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextName" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Address"

        android:id="@+id/textViewAddress" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextAddress" />



    <Button

        style="?android:attr/buttonStyleSmall"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Add"

        android:id="@+id/btnAdd" />



    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="View Peoples"

        android:id="@+id/btnView" />



</LinearLayout>
 
 
 
 
 
Output :


 ==========================================================================================
===========================================================================================

how to check   database  file  first   find  your  database
 file in android studio:


(1)First   go to  Tools   under   Tools  menu    then   under  that go to  
Android  then    go to "Android Device  Monitor".
(2)Then go to then under "Android Device Monitor" go to "File Explorer " :
(3)then go to  data again  inside data folder there is another data folder
 and under that go to your  project folder  "testing.om.com.testingdb"    
under project  package folder you will

get  databases folder ....

your will get   PersonDB  


(3)now go to   link   "http://sqlitebrowser.org/"  and download   
DB BROWSER FOR  SQLLite...

(4)install it and then open DB BROWSER AND OPEN YOUR DATABASE FILE  FIRST 
GO TO FILE MENU 
AND
OPEN DATABASE





  
================================================================================================================================================
Now  see complete  example  with insert,update ,delete  and display  :

(1)write code for   MainActivity.java file:
package net.simplifiedcoding.sqlitedbcode;



import android.content.Context;

import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;





public class MainActivity extends ActionBarActivity implements View.OnClickListener {



    private EditText editTextName;

    private EditText editTextAdd;

    private Button btnAdd;

    private Button btnView;



    private SQLiteDatabase db;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        createDatabase();



        editTextName = (EditText) findViewById(R.id.editTextName);

        editTextAdd = (EditText) findViewById(R.id.editTextAddress);



        btnAdd = (Button) findViewById(R.id.btnAdd);

        btnView = (Button) findViewById(R.id.btnView);



        btnAdd.setOnClickListener(this);

        btnView.setOnClickListener(this);

    }





    protected void createDatabase(){

        db=openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);

        db.execSQL("CREATE TABLE IF NOT EXISTS persons(id 
INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");

    }



    protected void insertIntoDB(){

        String name = editTextName.getText().toString().trim();

        String add = editTextAdd.getText().toString().trim();

        if(name.equals("") || add.equals("")){

            Toast.makeText(getApplicationContext(),"Please fill all fields"
Toast.LENGTH_LONG).show();

            return;

        }



        String query = "INSERT INTO persons (name,address) VALUES('"+name+"', '"+add+"');";

        db.execSQL(query);

        Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).show();

    }





    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;

    }



    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();



        //noinspection SimplifiableIfStatement

        if (id == R.id.action_settings) {

            return true;

        }



        return super.onOptionsItemSelected(item);

    }





    private void showPeoples(){

        Intent intent = new Intent(this,ViewPeople.class);

        startActivity(intent);

        finish();

    }



    @Override

    public void onClick(View v) {

        if(v == btnAdd){

            insertIntoDB();

        }

        if(v==btnView){

            showPeoples();

        }

    }

}
(2)now write code for  ViewPeople.java   file  :
package net.simplifiedcoding.sqlitedbcode; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class ViewPeople extends ActionBarActivity implements View.OnClickListener {     private EditText editTextName;     private EditText editTextAdd;     private EditText editTextId;     private Button btnPrev;     private Button btnNext;     private Button btnSave;     private Button btnDelete;     private static final String SELECT_SQL = "SELECT * FROM persons";     private SQLiteDatabase db;     private Cursor c;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_view_people);         openDatabase();         editTextId = (EditText) findViewById(R.id.editTextId);         editTextName = (EditText) findViewById(R.id.editTextName);         editTextAdd = (EditText) findViewById(R.id.editTextAddress);         btnPrev = (Button) findViewById(R.id.btnPrev);         btnNext = (Button) findViewById(R.id.btnNext);         btnSave = (Button) findViewById(R.id.btnSave);         btnDelete = (Button) findViewById(R.id.btnDelete);         btnNext.setOnClickListener(this);         btnPrev.setOnClickListener(this);         btnSave.setOnClickListener(this);         btnDelete.setOnClickListener(this);         c = db.rawQuery(SELECT_SQL, null);         c.moveToFirst();         showRecords();     }     protected void openDatabase() {         db = openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);     }     protected void showRecords() {         String id = c.getString(0);         String name = c.getString(1);         String add = c.getString(2);         editTextId.setText(id);         editTextName.setText(name);         editTextAdd.setText(add);     }     protected void moveNext() {         if (!c.isLast())             c.moveToNext();         showRecords();     }     protected void movePrev() {         if (!c.isFirst())             c.moveToPrevious();         showRecords();     }     protected void saveRecord() {         String name = editTextName.getText().toString().trim();         String add = editTextAdd.getText().toString().trim();         String id = editTextId.getText().toString().trim();         String sql = "UPDATE persons SET name='" + name + "', address='" + add + "' WHERE id=" + id + ";";         if (name.equals("") || add.equals("")) {             Toast.makeText(getApplicationContext(), "You cannot save blank values", Toast.LENGTH_LONG).show();             return;         }         db.execSQL(sql);         Toast.makeText(getApplicationContext(), "Records Saved Successfully", Toast.LENGTH_LONG).show();         c = db.rawQuery(SELECT_SQL, null);         c.moveToPosition(Integer.parseInt(id));     }     private void deleteRecord() {         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);         alertDialogBuilder.setMessage("Are you sure you want delete this person?");         alertDialogBuilder.setPositiveButton("Yes",                 new DialogInterface.OnClickListener() {                     @Override                     public void onClick(DialogInterface arg0, int arg1) {                         String id = editTextId.getText().toString().trim();                         String sql = "DELETE FROM persons WHERE id=" + id + ";";                         db.execSQL(sql);                         Toast.makeText(getApplicationContext(), "Record Deleted", Toast.LENGTH_LONG).show();                         c = db.rawQuery(SELECT_SQL,null);                     }                 });         alertDialogBuilder.setNegativeButton("No",                 new DialogInterface.OnClickListener() {                     @Override                     public void onClick(DialogInterface arg0, int arg1) {                     }                 });         AlertDialog alertDialog = alertDialogBuilder.create();         alertDialog.show();     }     @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.menu_view_people, menu);         return true;     }     @Override     public boolean onOptionsItemSelected(MenuItem item) {         // Handle action bar item clicks here. The action bar will         // automatically handle clicks on the Home/Up button, so long         // as you specify a parent activity in AndroidManifest.xml.         int id = item.getItemId();         //noinspection SimplifiableIfStatement         if (id == R.id.action_settings) {             return true;         }         return super.onOptionsItemSelected(item);     }     @Override     public void onClick(View v) {         if (v == btnNext) {             moveNext();         }         if (v == btnPrev) {             movePrev();         }         if (v == btnSave) {             saveRecord();         }         if (v == btnDelete) {             deleteRecord();         }     } }
(3)now write code for  activity_main.xml file:
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:orientation="vertical"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Name"

        android:id="@+id/textViewName" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextName" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Address"

        android:id="@+id/textViewAddress" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextAddress" />



    <Button

        style="?android:attr/buttonStyleSmall"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Add"

        android:id="@+id/btnAdd" />



    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="View Peoples"

        android:id="@+id/btnView" />

</LinearLayout>
(4)write code for   activity_view_people.xml file:
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:orientation="vertical"

    tools:context="net.simplifiedcoding.sqlitedbcode.ViewPeople">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="ID"

        android:id="@+id/textViewId" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:enabled="false"

        android:id="@+id/editTextId" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Name"

        android:id="@+id/textViewName" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextName" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Address"

        android:id="@+id/textViewAddress" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextAddress" />



    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:weightSum="1">



        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Previous"

            android:id="@+id/btnPrev"

            android:layout_weight="0.20" />



        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Save"

            android:id="@+id/btnSave"

            android:layout_weight="0.27" />





        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Next"

            android:id="@+id/btnNext"

            android:layout_weight="0.31" />



    </LinearLayout>



    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Delete"

        android:id="@+id/btnDelete" />



</LinearLayout>



Output:
first  add record  then 



============================================================================================
HOW  TO  DISPLAY  DATABASE    RECORD  INTO LISTVIEW:


Writing   code for     Inserting   records    into   database   :
(1)write   code for  Main2Activity.java  file:
package testing.om.com.listview;



import android.content.Context;

import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;





public class Main2Activity extends ActionBarActivity implements View.OnClickListener {



    private EditText editTextName;

    private EditText editTextAdd;

    private Button btnAdd;

    private Button btnView;



    private SQLiteDatabase db;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);



        createDatabase();



        editTextName = (EditText) findViewById(R.id.editTextName);

        editTextAdd = (EditText) findViewById(R.id.editTextAddress);



        btnAdd = (Button) findViewById(R.id.btnAdd);

        btnView = (Button) findViewById(R.id.btnView);



        btnAdd.setOnClickListener(this);

        btnView.setOnClickListener(this);

    }





    protected void createDatabase(){

        db=openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);

        db.execSQL("CREATE TABLE IF NOT EXISTS 
persons(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");

    }



    protected void insertIntoDB(){

        String name = editTextName.getText().toString().trim();

        String add = editTextAdd.getText().toString().trim();

        if(name.equals("") || add.equals("")){

            Toast.makeText(getApplicationContext(),"Please fill all fields", 
Toast.LENGTH_LONG).show();

            return;

        }



        String query = "INSERT INTO persons (name,address) VALUES('"+name+"', '"+add+"');";

        db.execSQL(query);

        Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).show();

    }





    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;

    }



    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();



        //noinspection SimplifiableIfStatement

        if (id == R.id.action_settings) {

            return true;

        }



        return super.onOptionsItemSelected(item);

    }





    private void showPeoples(){

        Intent intent = new Intent(this,Main2Activity.class);

        startActivity(intent);

        finish();

    }



    @Override

    public void onClick(View v) {

        if(v == btnAdd){

            insertIntoDB();

        }

        if(v==btnView){

            showPeoples();

        }

    }

}


(2)write code for activity_main2.xml file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context="testing.om.com.listview.Main2Activity">



    <android.support.design.widget.AppBarLayout 
android:layout_height="wrap_content"

        android:layout_width="match_parent" 
android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" 
app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main2" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>
 
 
(3)write code  for  content_main2.xml   file:
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:orientation="vertical"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Name"

        android:id="@+id/textViewName" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextName" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Address"

        android:id="@+id/textViewAddress" />



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextAddress" />



    <Button

        style="?android:attr/buttonStyleSmall"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Add"

        android:id="@+id/btnAdd" />



    <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="View Peoples"

        android:id="@+id/btnView" />

</LinearLayout>


Write   code to display record   to  listview
(1)write    code for  MainActivity.java    file  :
package testing.om.com.listview; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends AppCompatActivity {     private static final String SELECT_SQL = "SELECT * FROM persons";     private SQLiteDatabase db;     private Cursor c;     private ListView monthsListView;     private ArrayAdapter arrayAdapter;     /**      * Called when the activity is first created.      */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         openDatabase();         c = db.rawQuery(SELECT_SQL, null);         c.moveToFirst();         showRecords();     }     protected void openDatabase() {         db = openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);     }     protected void showRecords() {         String id = c.getString(0);         String name = c.getString(1);         String add = c.getString(2);          String[] monthsArray = {id,name,add};
        monthsListView = (ListView) findViewById(R.id.listView);         // this-The current activity context.         // Second param is the resource Id for list layout row item         // Third param is input array         arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, monthsArray);         monthsListView.setAdapter(arrayAdapter);     } }

(2)write    code    for     activity_main.xml       file :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout 
android:layout_height="wrap_content"

        android:layout_width="match_parent" 
android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary"
 app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write code  for  content_main.xml   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"

    xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent"

    android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_listview" tools:context=".listview">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textView" />



    <ListView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/listView"

        />

</RelativeLayout>


Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
    iPhone app training course in bangalore
    iPhone job oriented course in bangalore

    ReplyDelete

 

© 2013 ANDROID TRAINING IN MUMBAI BY OM SIR. All rights resevered. Designed by Templateism

Back To Top