Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

CP-38004 – Mobile Application Development (Android)

Due: 02/07/2020
Term: 2 (Online) May - July 2021 Assignment -4 Prof: Jude
Class No: 181404 Name: Jacky 20 Points
PDF format only
1. Design a Simple SQLite application. (Sample designs are given below)

Note: Design with you own work. Your own design will be graded more.
Don’t copy from others. If more than with same coding will be rejected.
Don’t copy source code form Internet.

Screen Layout
Source Code – .xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textColor="#00FFFFFF" />
</LinearLayout>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<TextView
android:id="@+id/textView15"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID:"
android:textSize="22sp" />

<EditText
android:id="@+id/stid"
android:layout_width="203dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</LinearLayout>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<TextView
android:id="@+id/textView17"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name:"
android:textSize="22sp" />

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

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Course:"
android:textSize="22sp" />

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

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<TextView
android:id="@+id/textView21"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Age:"
android:textSize="22sp" />

<EditText
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />

</LinearLayout>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<TextView
android:id="@+id/textView25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="School:"
android:textSize="22sp" />

<EditText
android:id="@+id/school"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />

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

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

</LinearLayout>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:id="@+id/dbid"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/dbname"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/dbcourse"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/dbage"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/dbschool"
android:layout_width="80dp"
android:layout_height="wrap_content" />
</LinearLayout>
Source Code – .java
MainActivity.java
package com.example.database;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends Activity {
private static String DB_NAME = "mydb";
private EditText stid, name, course, age, school;
private ArrayList<Map<String, Object>> data;
private dbHelper dbHelper;
private SQLiteDatabase db;
private Cursor cursor;
private SimpleAdapter listAdapter;
private View view;
private ListView lv;
private Button add, modify, delete;
private Map<String, Object> item;
private String selId;
private ContentValues selCV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stid = findViewById(R.id.stid);
name = findViewById(R.id.name);
course = findViewById(R.id.course);
age = findViewById(R.id.age);
school = findViewById(R.id.school);
lv = findViewById(R.id.lv);
add = findViewById(R.id.add);
modify = findViewById(R.id.modify);
delete = findViewById(R.id.delete);

add.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
dbAdd();
dbFindAll();
}
});
modify.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
dbUpdate();
dbFindAll();
}
});
delete.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
dbDel();
dbFindAll();
}
});
dbHelper = new dbHelper(this, DB_NAME, null, 1);
db = dbHelper.getWritableDatabase();
data = new ArrayList<Map<String, Object>>();
dbFindAll();

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Map<String, Object> listItem = (Map<String, Object>) lv.getItemAtPosition(position);
stid.setText((String) listItem.get("stid"));
name.setText((String) listItem.get("name"));
course.setText((String) listItem.get("course"));
age.setText((String) listItem.get("age"));
school.setText((String) listItem.get("school"));
selId = (String) listItem.get("_id");
Log.i("mydbDemo", "id=" + selId);
}
});
}
protected void dbDel() {
String where = "_id=" + selId;
int i = db.delete(dbHelper.TB_NAME, where, null);
if (i > 0)
Log.i("myDbDemo", "Data deleted successfully");
else
Log.i("myDbDemo", "Data not deleted");
}
private void showList() {
listAdapter = new SimpleAdapter(this, data,
R.layout.listview, new String[]{"stid", "name","course", "age","school"}, new int[]{R.id.dbid, R.id.dbname,
R.id.dbcourse, R.id.dbage, R.id.dbschool});
lv.setAdapter(listAdapter);
}
protected void dbUpdate() {
ContentValues values = new ContentValues();
values.put("stid", stid.getText().toString().trim());
values.put("name", name.getText().toString().trim());
values.put("course", course.getText().toString().trim());
values.put("school", school.getText().toString().trim());
values.put("age", age.getText().toString().trim());
String where = "_id=" + selId;
int i = db.update(dbHelper.TB_NAME, values, where, null);
if (i > 0)
Log.i("myDbDemo", "Data updated successfully");
else
Log.i("myDbDemo", "Data not updated");
}
protected void dbAdd() {
ContentValues values = new ContentValues();
values.put("name", name.getText().toString().trim());
values.put("stid", stid.getText().toString().trim());
values.put("course", course.getText().toString().trim());
values.put("age", age.getText().toString().trim());
values.put("school", school.getText().toString().trim());
long rowid = db.insert(dbHelper.TB_NAME, null, values);
if (rowid == -1)
Log.i("myDbDemo", "Data insertion failure");
else
Log.i("myDbDemo", "Data inserted successfully" + rowid);
}
protected void dbFindAll() {
data.clear();
cursor = db.query(dbHelper.TB_NAME, null, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String id = cursor.getString(0);
String stid = cursor.getString(1);
String name = cursor.getString(2);
String course = cursor.getString(3);
String age = cursor.getString(4);
String school = cursor.getString(5);

item = new HashMap<String, Object>();


item.put("_id", id);
item.put("stid", stid);
item.put("name", name);
item.put("course", course);
item.put("age", age);
item.put("school", school);
data.add(item);
cursor.moveToNext();
}
showList();
}
}

dbHelper.java
package com.example.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class dbHelper extends SQLiteOpenHelper {


public static final String TB_NAME = "friends";

public dbHelper(Context context, String name, CursorFactory factory, int version) {


super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " +
TB_NAME + " ( _id integer primary key autoincrement," +
"stid integer," +
"name varchar," +
"course varchar," +
"age integer," +
"school varchar"+
") ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TB_NAME);
onCreate(db);
}
}
Output
Details All Records
ID
Student ID 425
Name Kiki
Course 38004
Phone No 885245625

Insert
Show All
Delete

You might also like