18/06/2013

Android Using Database Inset,Delete,View

Layout:

create_layout.xml :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Register Student...."
            android:layout_marginLeft="60dp"
            android:layout_marginTop="20dp"
            android:textSize="@dimen/wel_admin"
            />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <TextView
            android:id="@+id/t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stud id: "
            android:layout_marginLeft="20dp"
            android:layout_marginTop="40dp"
            android:textSize="@dimen/form_ele"/>
    <EditText
        android:id="@+id/accid"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="40dp"
        android:layout_toRightOf="@id/t1"
        android:layout_marginLeft="30dp"/>
    </RelativeLayout>

    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        <TextView
                android:id="@+id/t2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name: "
                android:layout_marginLeft="20dp"
                android:layout_marginTop="40dp"
                android:textSize="@dimen/form_ele"/>
        <EditText
                android:id="@+id/name"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_marginTop="40dp"
                android:layout_toRightOf="@id/t2"
                android:layout_marginLeft="10dp"/>
    </RelativeLayout>

    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        <TextView
                android:id="@+id/t3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Class: "
                android:layout_marginLeft="20dp"
                android:layout_marginTop="40dp"
                android:textSize="@dimen/form_ele"/>
        <EditText
                android:id="@+id/classes"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_marginTop="40dp"
                android:layout_toRightOf="@id/t3"
                android:layout_marginLeft="60dp"/>
    </RelativeLayout>
    <Button
        android:id="@+id/bCreate"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="130dp"
        android:text="Create"
        android:background="@drawable/admin_design"/>
</LinearLayout>

del_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete Student Id...."
            android:layout_marginLeft="60dp"
            android:layout_marginTop="20dp"
            android:textSize="@dimen/wel_admin"
            />
    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <TextView
                android:id="@+id/d1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Stud Id: "
                android:layout_marginLeft="20dp"
                android:layout_marginTop="60dp"
                android:textSize="@dimen/form_ele"/>
        <EditText
                android:id="@+id/acciddel"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_marginTop="60dp"
                android:layout_toRightOf="@id/t1"
                android:layout_marginLeft="110dp"/>
    </RelativeLayout>
    <Button
        android:id="@+id/bdel"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="130dp"
        android:text="Delete"
        android:background="@drawable/admin_design"/>
</LinearLayout>

view_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:id="@+id/v"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"  />
</LinearLayout>

Create.Java:

package com.example.databaseproject;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Create extends Activity {
    Button b;
    EditText t1;
    EditText t2;
    EditText t3;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_layout);
       
        b=(Button)findViewById(R.id.bCreate);
        t1=(EditText)findViewById(R.id.accid);
        t2=(EditText)findViewById(R.id.name);
        t3=(EditText)findViewById(R.id.classes);
       
        final Context context=this;
       
        try
        {
          db=openOrCreateDatabase("student",SQLiteDatabase.CREATE_IF_NECESSARY,null);        
          db.execSQL("CREATE TABLE stud (id integer PRIMARY KEY, name text, Class text)");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
       
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String s=t1.getText().toString();
                String s1=t2.getText().toString();
                String s2=t3.getText().toString();

                ContentValues values=new ContentValues();
               
                values.put("id",s);
                values.put("name",s1);
                values.put("Class",s2);
               
                if((db.insert("stud",null,values))!= -1)
                {
                    Toast.makeText(Create.this, "Inserted...", 2000).show();
                }
                else
                {
                    Toast.makeText(Create.this,"Already taken this Id..",2000).show();
                }
                t1.setText("");
                t2.setText("");
                t3.setText("");
                Intent i=new Intent(context,Admin.class);
                startActivity(i);
            }
        });
    }

}
Delete.Java:
package com.example.databaseproject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Del extends Activity
{
    Button b;
    EditText e;
    SQLiteDatabase db;
    SQLiteOpenHelper d;

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

        b=(Button)findViewById(R.id.bdel);
        e=(EditText)findViewById(R.id.acciddel);
       
        final Context context=this;
       
        try
        {
           
            db=openOrCreateDatabase("student",SQLiteDatabase.CREATE_IF_NECESSARY,null);


        }
        catch(SQLiteException e)
        {
          e.printStackTrace();
          System.out.print("ERROR.............");
        }
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                String t=(e.getText().toString());
               
               try
                {
               String d="DELETE FROM stud WHERE id="+t;
               db.execSQL(d);
               String idt="id";
                }
                catch(Exception e)
                {
                   System.out.print("Error..................");
                }              
                e.setText("");
                Toast.makeText(Del.this, "Deleted...", 2000).show();
                Intent i=new Intent(context,Admin.class);
                startActivity(i);

            }
        });
    }

}
View.Java
package com.example.databaseproject;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.widget.TextView;



public class ViewAcc extends Activity
{
    SQLiteDatabase db;

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

            db=openOrCreateDatabase("student",SQLiteDatabase.CREATE_IF_NECESSARY,null);
            Cursor c= db.rawQuery("SELECT * FROM stud",null);

            TextView v=(TextView)findViewById(R.id.v);
            c.moveToFirst();

            String temp="";
           
            while(! c.isAfterLast())
            {
                String s2=c.getString(0);
                String s3=c.getString(1);
                String s4=c.getString(2);
               
                temp=temp+"\n Id:"+s2+"\tname:"+s3+"\tClass:"+s4;
               
                c.moveToNext();
            }
            v.setText(temp);


        }
        catch(SQLiteException e)
        {

        }
    }

}
Color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="txt">#FFFFFF</color>
    <color name="bg">#454545</color>
</resources>

Dimens.xml:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="wel_admin">30dp</dimen>
    <dimen name="form_ele">20dp</dimen>
</resources>

String.xml:

<string name="admin">Admin</string>
    <string name="admintxt">Welcome Admin...</string>