Sunday, 27 January 2013

AutoCompleteTextView in Android

ScreenShots:





activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:ems="10"
        android:hint="enter keyword" />

    <Button
        android:id="@+id/display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display" />

</LinearLayout>

text.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:textColor="#000000"
    android:textStyle="italic"
    android:textSize="20dp">

</TextView>

MainActivity.java:
package com.autocompletetextview.ram;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
String selectedname;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.textview, COUNTRIES);

auto.setAdapter(adapter);

auto.setOnItemClickListener(this);

// selectedname = auto.getText().toString();

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

display.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"selected name :" + selectedname, 3000).show();

}
});

}

static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
"Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",
"Antarctica", "Antigua and Barbuda", "Argentina", "Armenia",
"Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
"Botswana", "Bouvet Island", "Brazil",
"British Indian Ocean Territory", "British Virgin Islands",
"Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire",
"Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",
"Central African Republic", "Chad", "Chile", "China",
"Christmas Island", "Cocos (Keeling) Islands", "Colombia",
"Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia",
"Cuba", "Cyprus", "Czech Republic",
"Democratic Republic of the Congo", "Denmark", "Djibouti",
"Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt",
"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
"Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji",
"Finland", "Former Yugoslav Republic of Macedonia", "France",
"French Guiana", "French Polynesia", "French Southern Territories",
"Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
"Guinea", "Guinea-Bissau", "Guyana", "Haiti",
"Heard Island and McDonald Islands", "Honduras", "Hong Kong",
"Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq",
"Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
"Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar",
"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"Marshall Islands", "Martinique", "Mauritania", "Mauritius",
"Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia",
"Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
"Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria",
"Niue", "Norfolk Island", "North Korea", "Northern Marianas",
"Norway", "Oman", "Pakistan", "Palau", "Panama",
"Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
"Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe",
"Saint Helena", "Saint Kitts and Nevis", "Saint Lucia",
"Saint Pierre and Miquelon", "Saint Vincent and the Grenadines",
"Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles",
"Sierra Leone", "Singapore", "Slovakia", "Slovenia",
"Solomon Islands", "Somalia", "South Africa",
"South Georgia and the South Sandwich Islands", "South Korea",
"Spain", "Sri Lanka", "Sudan", "Suriname",
"Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland",
"Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand",
"The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga",
"Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
"Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
"Ukraine", "United Arab Emirates", "United Kingdom",
"United States", "United States Minor Outlying Islands", "Uruguay",
"Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
"Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia",
"Zambia", "Zimbabwe" };

public void onItemClick(AdapterView<?> adapterview, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
selectedname = (String) adapterview.getItemAtPosition(position);

}
}







Spinner in Android

ScreenShots:


activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="55dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="ShowSelectedMonth" />

</LinearLayout>

text.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#ff00ff"
    android:textSize="20dp" >

</TextView>

MainActivity.java:
package com.ram.spinnerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemSelectedListener {

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

final Spinner spinner = (Spinner) findViewById(R.id.spinner1);

Button showBtn = (Button) findViewById(R.id.button1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.text, MONTHS);

spinner.setAdapter(adapter);

// cannot use this interface for spinner
// spinner.setOnItemClickListener(this);

spinner.setOnItemSelectedListener(this);

showBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = spinner.getSelectedItemPosition();

Toast.makeText(getApplicationContext(),
"selected month is :" + MONTHS[position],
Toast.LENGTH_SHORT).show();
}
});

}

String[] MONTHS = { "jan", "feb", "mar", "april", "may", "june", "july",
"aug", "sep", "oct", "nov", "dec" };

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"selected month is :" + MONTHS[arg2], Toast.LENGTH_SHORT)
.show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

}










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>