MyApp.java
package dharan.dharani.com.myapplication;
/**
* Created by vdharanidharan on 04-Jul-17.
*/
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import static android.R.id.message;
import static dharan.dharani.com.myapplication.R.attr.title;
import static dharan.dharani.com.myapplication.R.string.view;
public class MyApp extends Activity implements OnClickListener
{
EditText editRollno,editName,editMarks;
Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editRollno=(EditText)findViewById(R.id.editRollno);
editName=(EditText)findViewById(R.id.editName);
editMarks=(EditText)findViewById(R.id.editMarks);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnView=(Button)findViewById(R.id.btnView);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnView.setOnClickListener(this);
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
}
public void onClick(View view) {
if (view == btnAdd) {
if (editRollno.getText().toString().trim().length() == 0 ||
editName.getText().toString().trim().length() == 0 ||
editMarks.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('" + editRollno.getText() + "','" + editName.getText() +
"','" + editMarks.getText() + "');");
showMessage("Success", "Record added To sql lite database");
clearText();
}
if(view == btnDelete)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnView)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
editName.setText(c.getString(1));
editMarks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
editRollno.setText("");
editName.setText("");
editMarks.setText("");
editRollno.requestFocus();
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/title"
android:layout_x="110dp"
android:layout_y="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp" />
<TextView android:text="@string/roll_no"
android:layout_x="30dp"
android:layout_y="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView android:text="@string/name"
android:layout_x="30dp"
android:layout_y="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView android:text="@string/marks"
android:layout_x="30dp"
android:layout_y="150dp"
android:layout_width="134dp"
android:layout_height="50dp"
android:textSize="18sp" />
<Button android:id="@+id/btnView"
android:text="@string/view"
android:layout_x="115dp"
android:layout_y="366dp"
android:layout_width="138dp"
android:layout_height="73dp"/>
<Button android:id="@+id/btnAdd"
android:text="@string/add"
android:layout_x="41dp"
android:layout_y="268dp"
android:layout_width="124dp"
android:layout_height="70dp"/>
<Button android:id="@+id/btnDelete"
android:text="@string/delete"
android:layout_x="234dp"
android:layout_y="264dp"
android:layout_width="120dp"
android:layout_height="68dp"/>
<EditText android:id="@+id/editRollno"
android:inputType="number"
android:layout_x="195dp"
android:layout_y="46dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<EditText android:id="@+id/editName"
android:inputType="text"
android:layout_x="187dp"
android:layout_y="90dp"
android:layout_width="150dp"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editMarks"
android:inputType="number"
android:layout_x="191dp"
android:layout_y="144dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
</AbsoluteLayout>
package dharan.dharani.com.myapplication;
/**
* Created by vdharanidharan on 04-Jul-17.
*/
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import static android.R.id.message;
import static dharan.dharani.com.myapplication.R.attr.title;
import static dharan.dharani.com.myapplication.R.string.view;
public class MyApp extends Activity implements OnClickListener
{
EditText editRollno,editName,editMarks;
Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editRollno=(EditText)findViewById(R.id.editRollno);
editName=(EditText)findViewById(R.id.editName);
editMarks=(EditText)findViewById(R.id.editMarks);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnView=(Button)findViewById(R.id.btnView);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnView.setOnClickListener(this);
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
}
public void onClick(View view) {
if (view == btnAdd) {
if (editRollno.getText().toString().trim().length() == 0 ||
editName.getText().toString().trim().length() == 0 ||
editMarks.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('" + editRollno.getText() + "','" + editName.getText() +
"','" + editMarks.getText() + "');");
showMessage("Success", "Record added To sql lite database");
clearText();
}
if(view == btnDelete)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnView)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
editName.setText(c.getString(1));
editMarks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
editRollno.setText("");
editName.setText("");
editMarks.setText("");
editRollno.requestFocus();
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/title"
android:layout_x="110dp"
android:layout_y="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp" />
<TextView android:text="@string/roll_no"
android:layout_x="30dp"
android:layout_y="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView android:text="@string/name"
android:layout_x="30dp"
android:layout_y="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView android:text="@string/marks"
android:layout_x="30dp"
android:layout_y="150dp"
android:layout_width="134dp"
android:layout_height="50dp"
android:textSize="18sp" />
<Button android:id="@+id/btnView"
android:text="@string/view"
android:layout_x="115dp"
android:layout_y="366dp"
android:layout_width="138dp"
android:layout_height="73dp"/>
<Button android:id="@+id/btnAdd"
android:text="@string/add"
android:layout_x="41dp"
android:layout_y="268dp"
android:layout_width="124dp"
android:layout_height="70dp"/>
<Button android:id="@+id/btnDelete"
android:text="@string/delete"
android:layout_x="234dp"
android:layout_y="264dp"
android:layout_width="120dp"
android:layout_height="68dp"/>
<EditText android:id="@+id/editRollno"
android:inputType="number"
android:layout_x="195dp"
android:layout_y="46dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<EditText android:id="@+id/editName"
android:inputType="text"
android:layout_x="187dp"
android:layout_y="90dp"
android:layout_width="150dp"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editMarks"
android:inputType="number"
android:layout_x="191dp"
android:layout_y="144dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
</AbsoluteLayout>
OUTPUT:
Thanks and regards,
Tech bird
Comments