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 :
- <?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">
- <EditText
- android:id="@+id/textName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="Name"
- android:inputType="textPersonName" />
- <EditText
- android:id="@+id/textNumber"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="Roll Number" />
- <EditText
- android:id="@+id/textMarks1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="Marks-1"
- android:inputType="numberDecimal" />
- <EditText
- android:id="@+id/textMarks2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="Marks-2"
- android:inputType="numberDecimal" />
- <Button
- android:id="@+id/button"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="insertIntoDatabase"
- android:text="Insert"
- android:textAllCaps="false" />
- <Button
- android:id="@+id/button2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="showInList"
- android:text="Show students list"
- android:textAllCaps="false" />
- <ListView
- android:id="@+id/studentsList"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
activity_test.xml :
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="84dp"
- android:onClick="startTask"
- android:text="Start Task" />
- </RelativeLayout>
list_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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/textName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentStart="true"
- android:layout_alignParentTop="true"
- android:layout_marginStart="25dp"
- android:layout_marginTop="18dp"
- android:text="TextView"
- android:textSize="20sp"
- android:textStyle="bold" />
- <TextView
- android:id="@+id/textMarks1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_alignTop="@+id/textName"
- android:layout_marginEnd="29dp"
- android:text="TextView" />
- <TextView
- android:id="@+id/textNumber"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="18dp"
- android:text="TextView"
- android:layout_below="@+id/textName"
- android:layout_alignStart="@+id/textName" />
- <TextView
- android:id="@+id/textMarks2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignStart="@+id/textMarks1"
- android:layout_alignTop="@+id/textNumber"
- android:text="TextView" />
- </RelativeLayout>
MainActivity.java :
- package com.example.saicharan.sqlitedatabase;
- import android.app.ProgressDialog;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- EditText textName, textNumber, textMarks1, textMarks2;
- ListView studentsList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textName = (EditText) findViewById(R.id.textName);
- textNumber = (EditText) findViewById(R.id.textNumber);
- textMarks1 = (EditText) findViewById(R.id.textMarks1);
- textMarks2 = (EditText) findViewById(R.id.textMarks2);
- studentsList = (ListView) findViewById(R.id.studentsList);
- }
- public void insertIntoDatabase(View view) {
- String name = textName.getText().toString();
- int number = Integer.parseInt(textNumber.getText().toString());
- float marks1 = Float.parseFloat(textMarks1.getText().toString());
- float marks2 = Float.parseFloat(textMarks2.getText().toString());
- Student student = new Student();
- student.setName(name);
- student.setRollNumber(number);
- student.setMarks1(marks1);
- student.setMarks2(marks2);
- MyDatabase database = new MyDatabase(this);
- database.insertStudent(student);
- Toast.makeText(this, "Data inserted", Toast.LENGTH_SHORT).show();
- textName.setText("");
- textNumber.setText("");
- textMarks1.setText("");
- textMarks2.setText("");
- }
- public void showInList(View view) {
- DataTask task = new DataTask();
- task.execute();
- }
- class DataTask extends AsyncTask<Integer, Void, ArrayList<Student>> {
- private ProgressDialog progressDialog;
- @Override
- protected void onPreExecute() {
- progressDialog = new ProgressDialog(MainActivity.this);
- progressDialog.setMessage("Please wait");
- progressDialog.show();
- }
- @Override
- protected ArrayList<Student> doInBackground(Integer... params) {
- MyDatabase database = new MyDatabase(MainActivity.this);
- ArrayList<Student> allStudents = database.getAllStudents();
- return allStudents;
- }
- @Override
- protected void onPostExecute(ArrayList<Student> students) {
- progressDialog.dismiss();
- StudentsAdapter adapter = new StudentsAdapter(MainActivity.this, students);
- studentsList.setAdapter(adapter);
- }
- }
- }
MyDatabase.java :
- package com.example.saicharan.sqlitedatabase;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.util.ArrayList;
- class MyDatabase extends SQLiteOpenHelper {
- private static final String DB_NAME = "student.db";
- private static final int VERSION = 1;
- public MyDatabase(Context context) {
- super(context, DB_NAME, null, VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- String studentTable = "CREATE TABLE STUDENT(ID INTEGER " +
- "PRIMARY KEY AUTOINCREMENT, NAME TEXT, ROLLNUMBER " +
- "INTEGER, MARKS1 REAL, MARKS2 REAL);";
- db.execSQL(studentTable);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public void insertStudent(Student student) {
- SQLiteDatabase database = getWritableDatabase();
- ContentValues cv = new ContentValues();
- cv.put("NAME", student.getName());
- cv.put("ROLLNUMBER", student.getRollNumber());
- cv.put("MARKS1", student.getMarks1());
- cv.put("MARKS2", student.getMarks2());
- database.insert("STUDENT", null, cv);
- }
- public ArrayList<Student> getAllStudents() {
- SQLiteDatabase database = getReadableDatabase();
- ArrayList<Student> students = new ArrayList<>();
- Cursor cursor = database.query("STUDENT", null, null, null, null, null, null);
- // Cursor cursor = database.rawQuery("SELECT * FROM STUDENT", null);
- boolean first = cursor.moveToFirst();
- if (!first) {
- return null;
- }
- int nameIndex = cursor.getColumnIndex("NAME");
- int numberIndex = cursor.getColumnIndex("ROLLNUMBER");
- int marks1Index = cursor.getColumnIndex("MARKS1");
- int marks2Index = cursor.getColumnIndex("MARKS2");
- while (!cursor.isAfterLast()) {
- String name = cursor.getString(nameIndex);
- int number = cursor.getInt(numberIndex);
- float marks1 = cursor.getFloat(marks1Index);
- float marks2 = cursor.getFloat(marks2Index);
- Student s = new Student();
- s.setName(name);
- s.setRollNumber(number);
- s.setMarks1(marks1);
- s.setMarks2(marks2);
- students.add(s);
- cursor.moveToNext();
- }
- return students;
- }
- }
Student.java :
- package com.example.saicharan.sqlitedatabase;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.util.ArrayList;
- class MyDatabase extends SQLiteOpenHelper {
- private static final String DB_NAME = "student.db";
- private static final int VERSION = 1;
- public MyDatabase(Context context) {
- super(context, DB_NAME, null, VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- String studentTable = "CREATE TABLE STUDENT(ID INTEGER " +
- "PRIMARY KEY AUTOINCREMENT, NAME TEXT, ROLLNUMBER " +
- "INTEGER, MARKS1 REAL, MARKS2 REAL);";
- db.execSQL(studentTable);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public void insertStudent(Student student) {
- SQLiteDatabase database = getWritableDatabase();
- ContentValues cv = new ContentValues();
- cv.put("NAME", student.getName());
- cv.put("ROLLNUMBER", student.getRollNumber());
- cv.put("MARKS1", student.getMarks1());
- cv.put("MARKS2", student.getMarks2());
- database.insert("STUDENT", null, cv);
- }
- public ArrayList<Student> getAllStudents() {
- SQLiteDatabase database = getReadableDatabase();
- ArrayList<Student> students = new ArrayList<>();
- Cursor cursor = database.query("STUDENT", null, null, null, null, null, null);
- // Cursor cursor = database.rawQuery("SELECT * FROM STUDENT", null);
- boolean first = cursor.moveToFirst();
- if (!first) {
- return null;
- }
- int nameIndex = cursor.getColumnIndex("NAME");
- int numberIndex = cursor.getColumnIndex("ROLLNUMBER");
- int marks1Index = cursor.getColumnIndex("MARKS1");
- int marks2Index = cursor.getColumnIndex("MARKS2");
- while (!cursor.isAfterLast()) {
- String name = cursor.getString(nameIndex);
- int number = cursor.getInt(numberIndex);
- float marks1 = cursor.getFloat(marks1Index);
- float marks2 = cursor.getFloat(marks2Index);
- Student s = new Student();
- s.setName(name);
- s.setRollNumber(number);
- s.setMarks1(marks1);
- s.setMarks2(marks2);
- students.add(s);
- cursor.moveToNext();
- }
- return students;
- }
- }
StudentsAdapter.java :
- package com.example.saicharan.sqlitedatabase;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.util.ArrayList;
- class MyDatabase extends SQLiteOpenHelper {
- private static final String DB_NAME = "student.db";
- private static final int VERSION = 1;
- public MyDatabase(Context context) {
- super(context, DB_NAME, null, VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- String studentTable = "CREATE TABLE STUDENT(ID INTEGER " +
- "PRIMARY KEY AUTOINCREMENT, NAME TEXT, ROLLNUMBER " +
- "INTEGER, MARKS1 REAL, MARKS2 REAL);";
- db.execSQL(studentTable);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- public void insertStudent(Student student) {
- SQLiteDatabase database = getWritableDatabase();
- ContentValues cv = new ContentValues();
- cv.put("NAME", student.getName());
- cv.put("ROLLNUMBER", student.getRollNumber());
- cv.put("MARKS1", student.getMarks1());
- cv.put("MARKS2", student.getMarks2());
- database.insert("STUDENT", null, cv);
- }
- public ArrayList<Student> getAllStudents() {
- SQLiteDatabase database = getReadableDatabase();
- ArrayList<Student> students = new ArrayList<>();
- Cursor cursor = database.query("STUDENT", null, null, null, null, null, null);
- // Cursor cursor = database.rawQuery("SELECT * FROM STUDENT", null);
- boolean first = cursor.moveToFirst();
- if (!first) {
- return null;
- }
- int nameIndex = cursor.getColumnIndex("NAME");
- int numberIndex = cursor.getColumnIndex("ROLLNUMBER");
- int marks1Index = cursor.getColumnIndex("MARKS1");
- int marks2Index = cursor.getColumnIndex("MARKS2");
- while (!cursor.isAfterLast()) {
- String name = cursor.getString(nameIndex);
- int number = cursor.getInt(numberIndex);
- float marks1 = cursor.getFloat(marks1Index);
- float marks2 = cursor.getFloat(marks2Index);
- Student s = new Student();
- s.setName(name);
- s.setRollNumber(number);
- s.setMarks1(marks1);
- s.setMarks2(marks2);
- students.add(s);
- cursor.moveToNext();
- }
- return students;
- }
- }
TestActivity.java :
- package com.example.saicharan.sqlitedatabase;
- import android.app.ProgressDialog;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Toast;
- public class TestActivity extends AppCompatActivity {
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_test);
- }
- public void startTask(View view) {
- ArithmeticTask task = new ArithmeticTask();
- Integer[] data = new Integer[]{10, 20};
- task.execute(data);
- }
- class ArithmeticTask extends AsyncTask<Integer, Void, Integer> {
- private ProgressDialog p;
- @Override
- protected void onPreExecute() {
- p = new ProgressDialog(TestActivity.this);
- p.setMessage("Please wait");
- p.show();
- }
- @Override
- protected Integer doInBackground(Integer... params) {
- Integer firstValue = params[0];
- Integer secondValue = params[1];
- Integer result = firstValue * secondValue;
- return result;
- }
- @Override
- protected void onPostExecute(Integer integer) {
- p.dismiss();
- Toast.makeText(TestActivity.this, String.valueOf(integer), Toast.LENGTH_LONG).show();
- }
- }
- }
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
Post a Comment