Sunday, 27 January 2013

SQLiteDatabase in Android(2)

homepage.xml:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Welcome to student database management system"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/insertbutton"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Insert"
        android:textSize="20dp" />

    <Button
        android:id="@+id/viewbutton"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="View"
        android:textSize="20dp" />

</LinearLayout>


insert.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/studenteditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student rollnumber"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/rollnumbereditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student mobile number"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/studentmobilenoeditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student email id"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/studentemailideditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter branch"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/studentbrancheditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <RadioGroup
            android:id="@+id/genderradioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/maleradio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

            <RadioButton
                android:id="@+id/femaleradio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />
        </RadioGroup>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/savebutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Save" />

            <Button
                android:id="@+id/cancelbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Cancel" />
        </TableRow>
    </LinearLayout>

</ScrollView>


viewhistory.xml:

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


update.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/updatestudenteditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student rollnumber"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/updaterollnumbereditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student mobile number"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/updatestudentmobilenoeditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter student email id"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/updatestudentemailideditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter branch"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/updatestudentbrancheditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <RadioGroup
            android:id="@+id/updategenderradioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/maleradio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

            <RadioButton
                android:id="@+id/femaleradio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />
        </RadioGroup>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/updatesavebutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Update" />

            <Button
                android:id="@+id/updatecancelbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Cancel" />
        </TableRow>
    </LinearLayout>

</ScrollView>


custom.xml:

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

    <TextView
        android:id="@+id/idtextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/studentnametextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/rollnumbertextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/mobilenumbertextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/emailidtextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/branchtextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/gendertextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


StudentDatabase.java:

package com.studentdbmsys.ram;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class StudentDatabase {

public static final String DATABASE_NAME = "studentdatabase";
public static final String TABLE_NAME = "studenttable";
public static final int DATABASE_VERSION = 1;

public static final String COL_ROWID = "rowid";
public static final String COL_STUDENTNAME = "studentname";
public static final String COL_ROLLNO = "rollno";
public static final String COL_MOBILENUMBER = "mobilenumber";
public static final String COL_EMAILID = "emailid";
public static final String COL_BRANCH = "branch";
public static final String COL_GENDER = "gender";

String CREATE_TABLE = "create table studenttable(rowid integer primary key autoincrement,studentname text not null,rollno text not null,mobilenumber text not null,emailid text not null,branch text not null,gender text not null)";

Context context;

SQLiteDatabase db;

DBHelper dbHelper;

public StudentDatabase(Context c) {
// TODO Auto-generated constructor stub
this.context = c;

dbHelper = new DBHelper(context);
}

StudentDatabase open() {

db = dbHelper.getWritableDatabase();
System.out.println("database opened....");
return this;
}

void close() {
dbHelper.close();
System.out.println("database closed......");
}

void insertValues(String studentname, String rollno, String mobilenumber,
String emailid, String branch, String gender) {
ContentValues con = new ContentValues();
con.put(COL_STUDENTNAME, studentname);
con.put(COL_ROLLNO, rollno);
con.put(COL_MOBILENUMBER, mobilenumber);
con.put(COL_EMAILID, emailid);
con.put(COL_BRANCH, branch);
con.put(COL_GENDER, gender);
System.out.println("values inseted.......");

 db.insert(TABLE_NAME, null, con);
}

Cursor getAllValues() {
String[] columns = { COL_ROWID, COL_STUDENTNAME, COL_ROLLNO,
COL_MOBILENUMBER, COL_EMAILID, COL_BRANCH, COL_GENDER };
return db.query(TABLE_NAME, columns, null, null, null, null, null);
}

void deleteAllRecords() {
db.delete(TABLE_NAME, null, null);
}

void deleteOneRecord(String rowid) {
db.delete(TABLE_NAME, rowid + "=" + COL_ROWID, null);
}

long upDateRecord(String rowid, String studentname, String rollno,
String mobilenumber, String emailid, String branch, String gender) {

Toast.makeText(context, studentname, 3000).show();
ContentValues con = new ContentValues();

con.put(COL_STUDENTNAME, studentname);
con.put(COL_ROLLNO, rollno);
con.put(COL_MOBILENUMBER, mobilenumber);
con.put(COL_EMAILID, emailid);
con.put(COL_BRANCH, branch);
con.put(COL_GENDER, gender);
System.out.println("values Updated successfully.......");
return  db.update(TABLE_NAME, con, rowid + "=" + COL_ROWID, null);
}

class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE);
System.out.println("studenttable created....");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

}


HomePage.java:

package com.studentdbmsys.ram;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HomePage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);

Button insertButton = (Button) findViewById(R.id.insertbutton);

insertButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), Insert.class));
}
});
Button viewHistory = (Button)findViewById(R.id.viewbutton);
 
viewHistory.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), ViewHistory.class));

}
});

}
}


Insert.java:

package com.studentdbmsys.ram;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Insert extends Activity {
EditText studentname, rollno, mobileno, emailid, branch;
RadioGroup genderGroup;
Button saveButton, cancelButton;
StudentDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);

db = new StudentDatabase(this);
db.open();

studentname = (EditText) findViewById(R.id.studenteditText);
rollno = (EditText) findViewById(R.id.rollnumbereditText);
mobileno = (EditText) findViewById(R.id.studentmobilenoeditText);
emailid = (EditText) findViewById(R.id.studentemailideditText);
branch = (EditText) findViewById(R.id.studentbrancheditText);

genderGroup = (RadioGroup) findViewById(R.id.genderradioGroup);

saveButton = (Button) findViewById(R.id.savebutton);
cancelButton = (Button) findViewById(R.id.cancelbutton);

saveButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String sname = studentname.getText().toString();
String srollno = rollno.getText().toString();
String smobileno = mobileno.getText().toString();
String smailid = emailid.getText().toString();
String sbranch = branch.getText().toString();

int selectedGenderID = genderGroup.getCheckedRadioButtonId();

RadioButton selectedGender = (RadioButton) findViewById(selectedGenderID);

String gender = selectedGender.getText().toString();

db.insertValues(sname, srollno, smobileno, smailid, sbranch,
gender);

Toast.makeText(getApplicationContext(), "saved successfulluy",
3000).show();

db.close();

finish();

startActivity(new Intent(getApplicationContext(),
HomePage.class));
}
});
cancelButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(),
HomePage.class));
}
});
}
}


ViewHistory.java:

package com.studentdbmsys.ram;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ViewHistory extends Activity {
StudentDatabase db;
ListView studentList;
StudentAdapter studentAdapter;
Cursor c;
int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewhistory);
db = new StudentDatabase(this);
db.open();

studentList = (ListView) findViewById(R.id.listView1);

updateList();

registerForContextMenu(studentList);

studentList.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
position = pos;
// TODO Auto-generated method stub
return false;
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("delete all history");
return super.onCreateOptionsMenu(menu);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
updateList();
super.onResume();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
db.deleteAllRecords();
Toast.makeText(getApplicationContext(), "data deleted successfully",
3000).show();
updateList();

return super.onOptionsItemSelected(item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
c.moveToPosition(position);
db.deleteOneRecord(c.getString(0));
updateList();
break;

case R.id.item2:
c.moveToPosition(position);
String rowidposition = c.getString(0);
int rowid = Integer.parseInt(rowidposition);
Bundle b = new Bundle();
b.putInt("recordposition", rowid);
startActivity(new Intent(getApplicationContext(),
UpDateRecord.class).putExtras(b));
break;
}
return super.onContextItemSelected(item);
}

class StudentAdapter extends BaseAdapter {
Context context;

public StudentAdapter(Context c) {
this.context = c;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return c.getCount();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.custom, null);

TextView t1 = (TextView) convertView.findViewById(R.id.idtextview);
TextView t2 = (TextView) convertView
.findViewById(R.id.studentnametextview);
TextView t3 = (TextView) convertView
.findViewById(R.id.rollnumbertextView);
TextView t4 = (TextView) convertView
.findViewById(R.id.mobilenumbertextview);
TextView t5 = (TextView) convertView
.findViewById(R.id.emailidtextview);
TextView t6 = (TextView) convertView
.findViewById(R.id.branchtextview);
TextView t7 = (TextView) convertView
.findViewById(R.id.gendertextview);

c.moveToPosition(position);

t1.setText(c.getString(0));
t2.setText(c.getString(1));
t3.setText(c.getString(2));
t4.setText(c.getString(3));
t5.setText(c.getString(4));
t6.setText(c.getString(5));
t7.setText(c.getString(6));

return convertView;
}

}

void updateList() {
c = db.getAllValues();

studentAdapter = new StudentAdapter(this);
studentList.setAdapter(studentAdapter);

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
db.close();
super.onDestroy();
}


}



UpDateRecord.java:
package com.studentdbmsys.ram;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class UpDateRecord extends Activity {
EditText studentname, rollno, mobileno, emailid, branch;
RadioGroup genderGroup;
Button updatesaveButton, updatecancelButton;
StudentDatabase db;
String rowid;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.update);

db = new StudentDatabase(this);
db.open();

Intent in = getIntent();

Bundle b = in.getExtras();

final int recordPosition = b.getInt("recordposition");
rowid = Integer.toString(recordPosition);

Toast.makeText(getApplicationContext(), rowid, 3000).show();

studentname = (EditText) findViewById(R.id.updatestudenteditText);
rollno = (EditText) findViewById(R.id.updaterollnumbereditText);
mobileno = (EditText) findViewById(R.id.updatestudentmobilenoeditText);
emailid = (EditText) findViewById(R.id.updatestudentemailideditText);
branch = (EditText) findViewById(R.id.updatestudentbrancheditText);

genderGroup = (RadioGroup) findViewById(R.id.updategenderradioGroup);

updatesaveButton = (Button) findViewById(R.id.updatesavebutton);
updatecancelButton = (Button) findViewById(R.id.updatecancelbutton);

updatesaveButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String sname = studentname.getText().toString();
String srollno = rollno.getText().toString();
String smobileno = mobileno.getText().toString();
String smailid = emailid.getText().toString();
String sbranch = branch.getText().toString();

int selectedGenderID = genderGroup.getCheckedRadioButtonId();

RadioButton selectedGender = (RadioButton) findViewById(selectedGenderID);

String gender = selectedGender.getText().toString();

db.upDateRecord(rowid, sname, srollno, smobileno, smailid,
sbranch, gender);

Toast.makeText(getApplicationContext(),
"updated successfulluy", 3000).show();

db.close();

finish();

startActivity(new Intent(getApplicationContext(),
ViewHistory.class));
}
});
updatecancelButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(),
ViewHistory.class));
}
});
}
}


AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.studentdbmsys.ram"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomePage"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="Insert"></activity>
        <activity android:name="ViewHistory"></activity>
        <activity android:name="UpDateRecord"></activity>
    </application>

</manifest>