0% found this document useful (0 votes)
103 views

SQLlite

The document describes an Android application for course registration. It includes XML layout files for the main activity, view activity, and edit activity screens. It also includes Java code for the MainActivity, View, and Edit classes. The application allows users to enter a name, course, and fee which are inserted into an SQLite database. It displays the records in a list view and allows editing of existing records by passing data between activities.

Uploaded by

Raja A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

SQLlite

The document describes an Android application for course registration. It includes XML layout files for the main activity, view activity, and edit activity screens. It also includes Java code for the MainActivity, View, and Edit classes. The application allows users to enter a name, course, and fee which are inserted into an SQLite database. It displays the records in a list view and allows editing of existing records by passing data between activities.

Uploaded by

Raja A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

activity_main.

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Registation"
android:textColor="@color/colorAccent"
android:textSize="30dp"
/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/name"
android:textAlignment="center"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/course"
android:textAlignment="center"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fee"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/fee"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/bt1"
android:text="Ok"
android:background="@color/colorPrimary"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/bt2"
android:text="View"
android:background="@color/colorAccent"
/>

</LinearLayout>
</LinearLayout>
MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText ed1,ed2,ed3;
Button b1,b2;

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

ed1 = findViewById(R.id.name);
ed2 = findViewById(R.id.course);
ed3 = findViewById(R.id.fee);

b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(),view.class);
startActivity(i);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insert();
}
});
}

public void insert()


{
try
{
String name = ed1.getText().toString();
String course = ed2.getText().toString();
String fee = ed3.getText().toString();

SQLiteDatabase db =
openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS records(id INTEGER PRIMARY
KEY AUTOINCREMENT,name VARCHAR,course VARCHAR,fee VARCHAR)");

String sql = "insert into records(name,course,fee)values(?,?,?)";


SQLiteStatement statement = db.compileStatement(sql);
statement.bindString(1,name);
statement.bindString(2,course);
statement.bindString(3,fee);
statement.execute();
Toast.makeText(this,"Record addded",Toast.LENGTH_LONG).show();

ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
}
catch (Exception ex)
{
Toast.makeText(this,"Record Fail",Toast.LENGTH_LONG).show();
}
}
}
activity_view.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lst1"

/>
</LinearLayout>

Create a Student Class

package com.example.kobinath.courseregapp;

public class student {

String id;
String name;
String course;
String fee;
String titles;
}
view.java

package com.example.kobinath.courseregapp;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class view extends AppCompatActivity {

ListView lst1;
ArrayList<String> titles = new ArrayList<String>();
ArrayAdapter arrayAdapter;

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

SQLiteDatabase db = openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);

lst1 = findViewById(R.id.lst1);
final Cursor c = db.rawQuery("select * from records",null);
int id = c.getColumnIndex("id");
int name = c.getColumnIndex("name");
int course = c.getColumnIndex("course");
int fee = c.getColumnIndex("fee");
titles.clear();

arrayAdapter = new
ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,titles);
lst1.setAdapter(arrayAdapter);

final ArrayList<student> stud = new ArrayList<student>();

if(c.moveToFirst())
{
do{
student stu = new student();
stu.id = c.getString(id);
stu.name = c.getString(name);
stu.course = c.getString(course);
stu.fee = c.getString(fee);
stud.add(stu);

titles.add(c.getString(id) + " \t " + c.getString(name) + " \t " + c.getString(course) + "


\t " + c.getString(fee) );

} while(c.moveToNext());
arrayAdapter.notifyDataSetChanged();
lst1.invalidateViews();

lst1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String aa = titles.get(position).toString();
student stu = stud.get(position);
Intent i = new Intent(getApplicationContext(),edit.class);
i.putExtra("id",stu.id);
i.putExtra("name",stu.name);
i.putExtra("course",stu.course);
i.putExtra("fee",stu.fee);
startActivity(i);

}
});

}
}
activity_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".edit"
android:orientation="vertical"
android:gravity="center"
>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Registation"
android:textColor="@color/colorAccent"
android:textSize="30dp"

/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/id"
android:textAlignment="center"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/name"
android:textAlignment="center"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/course"
android:textAlignment="center"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fee"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/fee"
android:textAlignment="center"
/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/bt1"
android:text="Edit"
android:background="@color/colorPrimary"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/bt2"
android:text="Delete"
android:background="@color/colorAccent"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/bt3"
android:text="Back"
android:background="@color/colorPrimaryDark"
/>

</LinearLayout>

</LinearLayout>

Edit.java
public class edit extends AppCompatActivity {

EditText ed1,ed2,ed3,ed4;
Button b1,b2,b3;

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

ed1 = findViewById(R.id.name);
ed2 = findViewById(R.id.course);
ed3 = findViewById(R.id.fee);
ed4 = findViewById(R.id.id);

b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b3 = findViewById(R.id.bt3);

Intent i = getIntent();
String t1 = i.getStringExtra("id").toString();
String t2 = i.getStringExtra("name").toString();
String t3 = i.getStringExtra("course").toString();
String t4 = i.getStringExtra("fee").toString();

ed4.setText(t1);
ed1.setText(t2);
ed2.setText(t3);
ed3.setText(t4);

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Delete();
}
});

b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent i = new Intent(getApplicationContext(),view.class);


startActivity(i);

}
});

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Edit();
}
});

public void Delete()


{
try
{
String id = ed4.getText().toString();
SQLiteDatabase db =
openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);

String sql = "delete from records where id = ?";


SQLiteStatement statement = db.compileStatement(sql);

statement.bindString(1,id);
statement.execute();
Toast.makeText(this,"Record Deleted",Toast.LENGTH_LONG).show();

ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();

}
catch (Exception ex)
{
Toast.makeText(this,"Record Fail",Toast.LENGTH_LONG).show();
}
}
public void Edit()
{
try
{
String name = ed1.getText().toString();
String course = ed2.getText().toString();
String fee = ed3.getText().toString();
String id = ed4.getText().toString();

SQLiteDatabase db =
openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);

String sql = "update records set name = ?,course=?,fee=? where id= ?";
SQLiteStatement statement = db.compileStatement(sql);
statement.bindString(1,name);
statement.bindString(2,course);
statement.bindString(3,fee);
statement.bindString(4,id);
statement.execute();
Toast.makeText(this,"Record Updateddd",Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();

}
catch (Exception ex)
{
Toast.makeText(this,"Record Fail",Toast.LENGTH_LONG).show();
}

You might also like