SQLiteDatabase

An application which deals with Database, Custom ListView :

This is a simple application which covers some of the basic topics of android like AsyncTask, SQLiteDatabase-insertion, Custom ListView

UI's of app:                                                                 

                 


activity_main.xml :

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

  6.     <EditText
  7.         android:id="@+id/textName"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:ems="10"
  11.         android:hint="Name"
  12.         android:inputType="textPersonName" />

  13.     <EditText
  14.         android:id="@+id/textNumber"
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content"
  17.         android:ems="10"
  18.         android:hint="Roll Number" />

  19.     <EditText
  20.         android:id="@+id/textMarks1"
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content"
  23.         android:ems="10"
  24.         android:hint="Marks-1"
  25.         android:inputType="numberDecimal" />

  26.     <EditText
  27.         android:id="@+id/textMarks2"
  28.         android:layout_width="match_parent"
  29.         android:layout_height="wrap_content"
  30.         android:ems="10"
  31.         android:hint="Marks-2"
  32.         android:inputType="numberDecimal" />

  33.     <Button
  34.         android:id="@+id/button"
  35.         android:layout_width="match_parent"
  36.         android:layout_height="wrap_content"
  37.         android:onClick="insertIntoDatabase"
  38.         android:text="Insert"
  39.         android:textAllCaps="false" />

  40.     <Button
  41.         android:id="@+id/button2"
  42.         android:layout_width="match_parent"
  43.         android:layout_height="wrap_content"
  44.         android:onClick="showInList"
  45.         android:text="Show students list"
  46.         android:textAllCaps="false" />

  47.     <ListView
  48.         android:id="@+id/studentsList"
  49.         android:layout_width="match_parent"
  50.         android:layout_height="match_parent" />
  51. </LinearLayout>


activity_test.xml :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">

  6.     <Button
  7.         android:id="@+id/button3"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_alignParentTop="true"
  11.         android:layout_centerHorizontal="true"
  12.         android:layout_marginTop="84dp"
  13.         android:onClick="startTask"
  14.         android:text="Start Task" />
  15. </RelativeLayout>


list_item.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">

  6.     <TextView
  7.         android:id="@+id/textName"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_alignParentStart="true"
  11.         android:layout_alignParentTop="true"
  12.         android:layout_marginStart="25dp"
  13.         android:layout_marginTop="18dp"
  14.         android:text="TextView"
  15.         android:textSize="20sp"
  16.         android:textStyle="bold" />

  17.     <TextView
  18.         android:id="@+id/textMarks1"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:layout_alignParentEnd="true"
  22.         android:layout_alignTop="@+id/textName"
  23.         android:layout_marginEnd="29dp"
  24.         android:text="TextView" />

  25.     <TextView
  26.         android:id="@+id/textNumber"
  27.         android:layout_width="wrap_content"
  28.         android:layout_height="wrap_content"
  29.         android:layout_marginTop="18dp"
  30.         android:text="TextView"
  31.         android:layout_below="@+id/textName"
  32.         android:layout_alignStart="@+id/textName" />

  33.     <TextView
  34.         android:id="@+id/textMarks2"
  35.         android:layout_width="wrap_content"
  36.         android:layout_height="wrap_content"
  37.         android:layout_alignStart="@+id/textMarks1"
  38.         android:layout_alignTop="@+id/textNumber"
  39.         android:text="TextView" />
  40. </RelativeLayout>


MainActivity.java :

  1. package com.example.saicharan.sqlitedatabase;

  2. import android.app.ProgressDialog;
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.ListView;
  9. import android.widget.Toast;

  10. import java.util.ArrayList;

  11. public class MainActivity extends AppCompatActivity {

  12.     EditText textName, textNumber, textMarks1, textMarks2;
  13.     ListView studentsList;

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

  18.         textName = (EditText) findViewById(R.id.textName);
  19.         textNumber = (EditText) findViewById(R.id.textNumber);
  20.         textMarks1 = (EditText) findViewById(R.id.textMarks1);
  21.         textMarks2 = (EditText) findViewById(R.id.textMarks2);

  22.         studentsList = (ListView) findViewById(R.id.studentsList);
  23.     }

  24.     public void insertIntoDatabase(View view) {
  25.         String name = textName.getText().toString();
  26.         int number = Integer.parseInt(textNumber.getText().toString());
  27.         float marks1 = Float.parseFloat(textMarks1.getText().toString());
  28.         float marks2 = Float.parseFloat(textMarks2.getText().toString());

  29.         Student student = new Student();
  30.         student.setName(name);
  31.         student.setRollNumber(number);
  32.         student.setMarks1(marks1);
  33.         student.setMarks2(marks2);

  34.         MyDatabase database = new MyDatabase(this);
  35.         database.insertStudent(student);
  36.         Toast.makeText(this, "Data inserted", Toast.LENGTH_SHORT).show();

  37.         textName.setText("");
  38.         textNumber.setText("");
  39.         textMarks1.setText("");
  40.         textMarks2.setText("");
  41.     }

  42.     public void showInList(View view) {
  43.         DataTask task = new DataTask();
  44.         task.execute();
  45.     }

  46.     class DataTask extends AsyncTask<Integer, Void, ArrayList<Student>> {

  47.         private ProgressDialog progressDialog;

  48.         @Override
  49.         protected void onPreExecute() {
  50.             progressDialog = new ProgressDialog(MainActivity.this);
  51.             progressDialog.setMessage("Please wait");
  52.             progressDialog.show();
  53.         }

  54.         @Override
  55.         protected ArrayList<Student> doInBackground(Integer... params) {
  56.             MyDatabase database = new MyDatabase(MainActivity.this);
  57.             ArrayList<Student> allStudents = database.getAllStudents();
  58.             return allStudents;
  59.         }

  60.         @Override
  61.         protected void onPostExecute(ArrayList<Student> students) {
  62.             progressDialog.dismiss();

  63.             StudentsAdapter adapter = new StudentsAdapter(MainActivity.this, students);
  64.             studentsList.setAdapter(adapter);
  65.         }
  66.     }
  67. }


MyDatabase.java :

  1. package com.example.saicharan.sqlitedatabase;

  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;

  7. import java.util.ArrayList;

  8. class MyDatabase extends SQLiteOpenHelper {

  9.     private static final String DB_NAME = "student.db";
  10.     private static final int VERSION = 1;

  11.     public MyDatabase(Context context) {
  12.         super(context, DB_NAME, null, VERSION);
  13.     }

  14.     @Override
  15.     public void onCreate(SQLiteDatabase db) {
  16.         String studentTable = "CREATE TABLE STUDENT(ID INTEGER " +
  17.                 "PRIMARY KEY AUTOINCREMENT, NAME TEXT, ROLLNUMBER " +
  18.                 "INTEGER, MARKS1 REAL, MARKS2 REAL);";

  19.         db.execSQL(studentTable);
  20.     }

  21.     @Override
  22.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  23.     }

  24.     public void insertStudent(Student student) {
  25.         SQLiteDatabase database = getWritableDatabase();

  26.         ContentValues cv = new ContentValues();
  27.         cv.put("NAME", student.getName());
  28.         cv.put("ROLLNUMBER", student.getRollNumber());
  29.         cv.put("MARKS1", student.getMarks1());
  30.         cv.put("MARKS2", student.getMarks2());

  31.         database.insert("STUDENT", null, cv);
  32.     }

  33.     public ArrayList<Student> getAllStudents() {
  34.         SQLiteDatabase database = getReadableDatabase();

  35.         ArrayList<Student> students = new ArrayList<>();

  36.         Cursor cursor = database.query("STUDENT", null, null, null, null, null, null);
  37.         //        Cursor cursor = database.rawQuery("SELECT * FROM STUDENT", null);
  38.         boolean first = cursor.moveToFirst();
  39.         if (!first) {
  40.             return null;
  41.         }

  42.         int nameIndex = cursor.getColumnIndex("NAME");
  43.         int numberIndex = cursor.getColumnIndex("ROLLNUMBER");
  44.         int marks1Index = cursor.getColumnIndex("MARKS1");
  45.         int marks2Index = cursor.getColumnIndex("MARKS2");

  46.         while (!cursor.isAfterLast()) {
  47.             String name = cursor.getString(nameIndex);
  48.             int number = cursor.getInt(numberIndex);
  49.             float marks1 = cursor.getFloat(marks1Index);
  50.             float marks2 = cursor.getFloat(marks2Index);

  51.             Student s = new Student();
  52.             s.setName(name);
  53.             s.setRollNumber(number);
  54.             s.setMarks1(marks1);
  55.             s.setMarks2(marks2);

  56.             students.add(s);

  57.             cursor.moveToNext();
  58.         }
  59.         return students;
  60.     }
  61. }


Student.java :

  1. package com.example.saicharan.sqlitedatabase;

  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;

  7. import java.util.ArrayList;

  8. class MyDatabase extends SQLiteOpenHelper {

  9.     private static final String DB_NAME = "student.db";
  10.     private static final int VERSION = 1;

  11.     public MyDatabase(Context context) {
  12.         super(context, DB_NAME, null, VERSION);
  13.     }

  14.     @Override
  15.     public void onCreate(SQLiteDatabase db) {
  16.         String studentTable = "CREATE TABLE STUDENT(ID INTEGER " +
  17.                 "PRIMARY KEY AUTOINCREMENT, NAME TEXT, ROLLNUMBER " +
  18.                 "INTEGER, MARKS1 REAL, MARKS2 REAL);";

  19.         db.execSQL(studentTable);
  20.     }

  21.     @Override
  22.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  23.     }

  24.     public void insertStudent(Student student) {
  25.         SQLiteDatabase database = getWritableDatabase();

  26.         ContentValues cv = new ContentValues();
  27.         cv.put("NAME", student.getName());
  28.         cv.put("ROLLNUMBER", student.getRollNumber());
  29.         cv.put("MARKS1", student.getMarks1());
  30.         cv.put("MARKS2", student.getMarks2());

  31.         database.insert("STUDENT", null, cv);
  32.     }

  33.     public ArrayList<Student> getAllStudents() {
  34.         SQLiteDatabase database = getReadableDatabase();

  35.         ArrayList<Student> students = new ArrayList<>();

  36.         Cursor cursor = database.query("STUDENT", null, null, null, null, null, null);
  37.         //        Cursor cursor = database.rawQuery("SELECT * FROM STUDENT", null);
  38.         boolean first = cursor.moveToFirst();
  39.         if (!first) {
  40.             return null;
  41.         }

  42.         int nameIndex = cursor.getColumnIndex("NAME");
  43.         int numberIndex = cursor.getColumnIndex("ROLLNUMBER");
  44.         int marks1Index = cursor.getColumnIndex("MARKS1");
  45.         int marks2Index = cursor.getColumnIndex("MARKS2");

  46.         while (!cursor.isAfterLast()) {
  47.             String name = cursor.getString(nameIndex);
  48.             int number = cursor.getInt(numberIndex);
  49.             float marks1 = cursor.getFloat(marks1Index);
  50.             float marks2 = cursor.getFloat(marks2Index);

  51.             Student s = new Student();
  52.             s.setName(name);
  53.             s.setRollNumber(number);
  54.             s.setMarks1(marks1);
  55.             s.setMarks2(marks2);

  56.             students.add(s);

  57.             cursor.moveToNext();
  58.         }
  59.         return students;
  60.     }
  61. }

StudentsAdapter.java :

  1. package com.example.saicharan.sqlitedatabase;

  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;

  7. import java.util.ArrayList;

  8. class MyDatabase extends SQLiteOpenHelper {

  9.     private static final String DB_NAME = "student.db";
  10.     private static final int VERSION = 1;

  11.     public MyDatabase(Context context) {
  12.         super(context, DB_NAME, null, VERSION);
  13.     }

  14.     @Override
  15.     public void onCreate(SQLiteDatabase db) {
  16.         String studentTable = "CREATE TABLE STUDENT(ID INTEGER " +
  17.                 "PRIMARY KEY AUTOINCREMENT, NAME TEXT, ROLLNUMBER " +
  18.                 "INTEGER, MARKS1 REAL, MARKS2 REAL);";

  19.         db.execSQL(studentTable);
  20.     }

  21.     @Override
  22.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  23.     }

  24.     public void insertStudent(Student student) {
  25.         SQLiteDatabase database = getWritableDatabase();

  26.         ContentValues cv = new ContentValues();
  27.         cv.put("NAME", student.getName());
  28.         cv.put("ROLLNUMBER", student.getRollNumber());
  29.         cv.put("MARKS1", student.getMarks1());
  30.         cv.put("MARKS2", student.getMarks2());

  31.         database.insert("STUDENT", null, cv);
  32.     }

  33.     public ArrayList<Student> getAllStudents() {
  34.         SQLiteDatabase database = getReadableDatabase();

  35.         ArrayList<Student> students = new ArrayList<>();

  36.         Cursor cursor = database.query("STUDENT", null, null, null, null, null, null);
  37.         //        Cursor cursor = database.rawQuery("SELECT * FROM STUDENT", null);
  38.         boolean first = cursor.moveToFirst();
  39.         if (!first) {
  40.             return null;
  41.         }

  42.         int nameIndex = cursor.getColumnIndex("NAME");
  43.         int numberIndex = cursor.getColumnIndex("ROLLNUMBER");
  44.         int marks1Index = cursor.getColumnIndex("MARKS1");
  45.         int marks2Index = cursor.getColumnIndex("MARKS2");

  46.         while (!cursor.isAfterLast()) {
  47.             String name = cursor.getString(nameIndex);
  48.             int number = cursor.getInt(numberIndex);
  49.             float marks1 = cursor.getFloat(marks1Index);
  50.             float marks2 = cursor.getFloat(marks2Index);

  51.             Student s = new Student();
  52.             s.setName(name);
  53.             s.setRollNumber(number);
  54.             s.setMarks1(marks1);
  55.             s.setMarks2(marks2);

  56.             students.add(s);

  57.             cursor.moveToNext();
  58.         }
  59.         return students;
  60.     }
  61. }

TestActivity.java :

  1. package com.example.saicharan.sqlitedatabase;

  2. import android.app.ProgressDialog;
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.annotation.Nullable;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.View;
  8. import android.widget.Toast;

  9. public class TestActivity extends AppCompatActivity {

  10.     @Override
  11.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_test);
  14.     }

  15.     public void startTask(View view) {
  16.         ArithmeticTask task = new ArithmeticTask();
  17.         Integer[] data = new Integer[]{10, 20};
  18.         task.execute(data);
  19.     }

  20.     class ArithmeticTask extends AsyncTask<Integer, Void, Integer> {

  21.         private ProgressDialog p;

  22.         @Override
  23.         protected void onPreExecute() {
  24.             p = new ProgressDialog(TestActivity.this);
  25.             p.setMessage("Please wait");
  26.             p.show();
  27.         }

  28.         @Override
  29.         protected Integer doInBackground(Integer... params) {
  30.             Integer firstValue = params[0];
  31.             Integer secondValue = params[1];

  32.             Integer result = firstValue * secondValue;
  33.             return result;
  34.         }

  35.         @Override
  36.         protected void onPostExecute(Integer integer) {
  37.             p.dismiss();

  38.             Toast.makeText(TestActivity.this, String.valueOf(integer), Toast.LENGTH_LONG).show();
  39.         }
  40.     }
  41. }                  

This application is just a demonstration of some of the topics of android so the design part is not given much importance. (Just look at the implementation not the UI of the app).If necessary change the UI according to needs
      The above application is available at https://github.com/papanisaicharan/android

Comments

Popular posts from this blog

Splitting criteria

QUOTATION SERVER AND CLIENT

How to Handle Noisy Data in preprocessing of data?