Monday, 9 February 2015

SQLiteDatabase Part2: inserting data into Table Example

Part 2: inserting data into tables

ScreenShots:







Step 1: add the following method in DBAdapter

DBAdapter.java

public long insertValues(String name, String contact) {
              ContentValues values = new ContentValues();
              values.put("name", name);
              values.put("phone", contact);
              return database.insert(DBOpenHelper.SAMPLE_TABLE_NAME, null, values);
       }

Step 2: Create InsertActivity and its respective xml write the following code in xml and Activity

activity_insert.xml

<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"
    tools:context=".InsertActivity" >

    <EditText
        android:id="@+id/insert_editText_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="Enter Your Name" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/insert_editText_contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/insert_editText_name"
        android:layout_below="@+id/insert_editText_name"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Enter Your Contact Number"
        android:inputType="phone" />

    <Button
        android:id="@+id/insert_button_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/insert_editText_contact"
        android:layout_below="@+id/insert_editText_contact"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="24dp"
        android:text="Submit"
        android:onClick="submit" />

    <Button
        android:id="@+id/insert_button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/insert_button_submit"
        android:layout_alignRight="@+id/insert_editText_contact"
        android:layout_marginRight="10dp"
        android:text="Cancel"
        android:onClick="cancel" />

</RelativeLayout>

InsertActivity.java

package com.swamys.databaseexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.swamys.database.dbadapter.DBAdapter;

public class InsertActivity extends Activity {

       EditText editText_name, editText_contact;
       String name, contact;
       DBAdapter dbAdapter;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_insert);

              // create references for all the four views
              editText_name = (EditText) findViewById(R.id.insert_editText_name);
              editText_contact = (EditText) findViewById(R.id.insert_editText_contact);

              // create reference for DBAdaper
              dbAdapter = new DBAdapter(getApplicationContext());
       }

       // submit button click listener
       public void submit(View view) {
              name = editText_name.getText().toString();
              contact = editText_contact.getText().toString();

              long response = dbAdapter.insertValues(name, contact);
              if (response == -1) {
                     Toast.makeText(getApplicationContext(), "Data failed to insert",
                                  Toast.LENGTH_LONG).show();
              } else {
                     Toast.makeText(getApplicationContext(),
                                  "Data inserted successfully", Toast.LENGTH_LONG).show();
              }

              finish();
       }

       // cancel button click listener
       public void cancel(View view) {
              finish();
       }

       @Override
       protected void onDestroy() {
              super.onDestroy();
              dbAdapter.closeConnection();
       }

}

Step 3: create button in activity_main.xml name it as insert

activity_main.xml

<Button
        android:id="@+id/main_button_insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Insert"
        android:onClick="insert" />


Step 4: add insert(View) method in MainActivity

MainActivity.java

public void insert(View view) {
              Intent intent = new Intent(getApplicationContext(),
                           InsertActivity.class);
              startActivity(intent);
       }

Step 5: Register InsertActivity in Manifest file

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.swamys.databaseexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.swamys.databaseexample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.swamys.databaseexample.InsertActivity"
            android:label="@string/title_activity_insert" >
        </activity>
    </application>

</manifest>

No comments:

Post a Comment