Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.myphonecaller;
- /*
- a "natural" representation of what we understand
- by "contact"
- */
- public class Contact {
- protected String mContactName;
- protected String mContactNumber;
- public Contact(
- String pName,
- String pNumber
- ){
- this.mContactName = pName;
- this.mContactNumber = pNumber;
- }//Contact
- @Override
- public String toString(){
- String strRet = String.format(
- "%s : %s",
- this.mContactName,
- this.mContactNumber
- );
- return strRet;
- }//toString
- public String getName(){
- return this.mContactName;
- }//getName
- public String getNumber(){
- return this.mContactNumber;
- }//getNumber
- }//Contact
- //*
- package com.joythis.android.myphonecaller;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import androidx.annotation.Nullable;
- import java.util.ArrayList;
- /*
- for the non-volatile representation of "contacts"
- following the framework, regarding inheritance from
- SQLiteOpenHelper
- */
- public class ContactDB extends SQLiteOpenHelper{
- public final static String DB_NAME = "contacts.db";
- public final static int DB_VERSION = 1;
- //create constants for all table and column names
- public final static String TABLE_CONTACTS = "tContacts";
- public final static String COL_ID = "_id";
- public final static String COL_NAME = "cName";
- public final static String COL_NUMBER = "cNumber";
- /*
- CREATE TABLE IF NOT EXISTS tContacts(
- _id INTEGER PRIMARY KEY AUTOINCREMENT,
- cName TEXT NOT NULL,
- cNumber TEXT NOT NULL
- );
- */
- public final static String CREATE_TABLE_CONTACTS =
- "CREATE TABLE IF NOT EXISTS "+TABLE_CONTACTS+"(\n"+
- COL_ID+" INTEGER PRIMARY KEY NOT NULL, \n"+
- COL_NAME+" TEXT NOT NULL, \n"+
- COL_NUMBER+" TEXT NOT NULL\n"+
- ");";
- /*
- DROP TABLE IF EXISTS tContacts;
- */
- public final static String DROP_TABLE_CONTACTS =
- "DROP TABLE IF EXISTS "+TABLE_CONTACTS+";";
- public ContactDB(
- Context pContext
- ){
- super(
- pContext,
- DB_NAME,
- null,
- DB_VERSION
- );
- }//ContactDB
- public ContactDB(
- @Nullable Context context,
- @Nullable String name,
- @Nullable SQLiteDatabase.CursorFactory factory,
- int version
- ){
- super(context, name, factory, version);
- }//ContactDB
- void installDB(
- SQLiteDatabase pDB
- ){
- if (pDB!=null){
- try{
- pDB.execSQL(
- CREATE_TABLE_CONTACTS
- );
- }//try
- catch (Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- }//if
- }//installDB
- /*
- will be AUTOMATICALLY called upon the first need to
- use the database; e.g. upon the first "insert", or the
- first "select".
- The developer will NEVER, EVER, call this method directly.
- */
- @Override
- public void onCreate(SQLiteDatabase db) {
- //perform database installation here
- installDB(db);
- }//onCreate
- /*
- will be AUTOMATICALLY called
- */
- @Override
- public void onUpgrade(
- SQLiteDatabase db,
- int oldVersion,
- int newVersion
- )
- {
- if (db!=null){
- //upgrade/change the database infrastructure here
- if (newVersion > oldVersion){
- //destroy the current infrastructure7
- //TODO: backup of the previous data
- try {
- db.execSQL(DROP_TABLE_CONTACTS);
- }//try
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- installDB(db);
- }//if
- }//if
- }//onUpgrade
- //insertContact
- /*
- will return the "line" where the record is inserted
- */
- public final static String KEY_NAME = "KEY_NAME";
- public final static String KEY_NUMBER = "KEY_NUMBER";
- public long insertContact (Contact pC)
- {
- SQLiteDatabase db = this.getWritableDatabase();
- if (db!=null){
- ContentValues pairsKeyValue = new ContentValues();
- pairsKeyValue.put(
- KEY_NAME,
- pC.getName()
- ); //contact name
- pairsKeyValue.put(
- KEY_NUMBER,
- pC.getNumber()
- ); //contact number
- //-1 on failure
- long iRecordNumber =
- db.insert(
- TABLE_CONTACTS,
- null,
- pairsKeyValue
- );
- return iRecordNumber;
- }//if
- return -1;
- }//insertContact
- /*
- public long insertContact(
- String pStrName, String pStrNumber
- ){
- }//insertContact
- */
- public final static String SELECT_ALL_CONTACTS_BY_NAME_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" ASC;";
- public final static String SELECT_ALL_CONTACTS_BY_NAME_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" DESC;";
- public final static String SELECT_ALL_CONTACTS_BY_NUMBER_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" ASC;";
- public final static String SELECT_ALL_CONTACTS_BY_NUMBER_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" DESC;";
- public final static String SELECT_ALL_CONTACTS_BY_INSERT_DATE_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" ASC;";
- public final static String SELECT_ALL_CONTACTS_BY_INSERT_DATE_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" DESC;";
- public ArrayList<Contact> selectAllContactsByNameASC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NAME_ASC);
- }//selectAllContactsByNameASC
- public ArrayList<Contact> selectAllContactsByNameDESC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NAME_DESC);
- }//selectAllContactsByNameASC
- public ArrayList<Contact> selectAllContactsByNumberASC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NUMBER_ASC);
- }//selectAllContactsByNumberASC
- public ArrayList<Contact> selectAllContactsByNumberDESC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NUMBER_DESC);
- }//selectAllContactsByNumberASC
- public ArrayList<Contact> selectAllContactsByInsertDateASC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_INSERT_DATE_ASC);
- }//selectAllContactsByNameASC
- public ArrayList<Contact> selectAllContactsByInsertDateDESC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_INSERT_DATE_DESC);
- }//selectAllContactsByNameASC
- private ArrayList<Contact> selectAllByQuery(
- String pSelectQuery
- ){
- ArrayList<Contact> aRet = new ArrayList<>();
- boolean bCheck = pSelectQuery.length()>0;
- if (bCheck){
- SQLiteDatabase db = getReadableDatabase();
- if (db!=null){
- Cursor cursor =
- db.rawQuery(
- pSelectQuery,
- null
- );
- if (cursor!=null){
- cursor.moveToFirst();
- while(!cursor.isAfterLast()){
- String strName =
- cursor.getString(
- cursor.getColumnIndex(COL_NAME)
- //which column INDEX?
- );
- String strNumber =
- cursor.getString(
- cursor.getColumnIndex(COL_NUMBER)
- //which column INDEX?
- );
- Contact c = new Contact(strName, strNumber);
- aRet.add(c);
- cursor.moveToNext();
- }//while
- }//if
- }//if can read the database
- }//if got a non empty query
- return aRet;
- }//selectAll
- }//ContactDB
- //**
- package com.joythis.android.myphonecaller;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- Context mContext;
- ContactDB mDB;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- void init(){
- mContext = this;
- //TODO:
- /*
- layout
- op layout
- */
- mDB = new ContactDB(this);
- Contact c = new Contact("Artur", "123");
- mDB.insertContact(c);
- ArrayList<Contact> all = mDB.selectAllContactsByNumberDESC();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment