am_dot_com

PDM2 20201124

Nov 24th, 2020 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.44 KB | None | 0 0
  1. package com.joythis.android.mycaller;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.util.Log;
  9.  
  10. import androidx.annotation.Nullable;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. public class ContactDB extends SQLiteOpenHelper {
  15.     public final static int CONTACTS_DB_VERSION = 33;
  16.     public final static String CONTACTS_DB_NAME = "CONTACTS.DB";
  17.  
  18.     public final static String TABLE_CONTACTS = "tContacts";
  19.  
  20.     public final static String COL_ID = "_id";
  21.     public final static String COL_NAME = "cName";
  22.     public final static String COL_NUMBER = "cNumber";
  23.  
  24.     public final static String DROP_CONTACTS =
  25.         "DROP TABLE IF EXISTS "+TABLE_CONTACTS+";";
  26.  
  27.     /*
  28.     create table if not exists tContacts(
  29.         _id integer primary key autoincrement,
  30.         cName text not null,
  31.         cNumber text not null
  32.     );
  33.      */
  34.     public final static String CREATE_TABLE_CONTACTS =
  35.         "CREATE TABLE IF NOT EXISTS "+
  36.         TABLE_CONTACTS+"(\n"+
  37.         COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,\n"+
  38.         COL_NAME+" TEXT NOT NULL,\n"+
  39.         COL_NUMBER+" TEXT NOT NULL\n"+
  40.         ");\n";
  41.  
  42.     public ContactDB(
  43.         @Nullable Context context
  44.     )
  45.     {
  46.         super(
  47.             context,
  48.             CONTACTS_DB_NAME,
  49.             null,
  50.             CONTACTS_DB_VERSION
  51.         );
  52.     }//ContactDB
  53.  
  54.     public ContactDB(
  55.         @Nullable Context context,
  56.         @Nullable String name,
  57.         @Nullable SQLiteDatabase.CursorFactory factory,
  58.         int version
  59.     )
  60.     {
  61.         super(context, name, factory, version);
  62.     }//ContactDB
  63.  
  64.     /*
  65.     this method will install the necessary
  66.     infrastructure (create the table(s))
  67.  
  68.     this method will be AUTOMATICALLY called by the
  69.     framework upon the 1st need to read or write data
  70.     in "Contact" related table(s)
  71.      */
  72.     @Override
  73.     public void onCreate(SQLiteDatabase db) {
  74.         installDB(db);
  75.     }//onCreate
  76.  
  77.     /*
  78.     will be automatically called whenever the database
  79.     version changes
  80.      */
  81.     @Override
  82.     public void onUpgrade
  83.     (SQLiteDatabase db, int oldVersion, int newVersion)
  84.     {
  85.         try {
  86.             if (newVersion > oldVersion) {
  87.                 db.execSQL(DROP_CONTACTS);
  88.                 installDB(db);
  89.             }
  90.         }
  91.         catch(Exception e){
  92.             Log.e(
  93.                getClass().getName(),
  94.                e.toString()
  95.             );
  96.         }
  97.     }//onUpgrade
  98.  
  99.     void installDB(SQLiteDatabase pDB){
  100.         if (pDB!=null){
  101.             try {
  102.                 pDB.execSQL(CREATE_TABLE_CONTACTS);
  103.             }
  104.             catch(Exception e){
  105.                 Log.e(
  106.                     getClass().getName(),
  107.                     e.toString()
  108.                 );
  109.             }
  110.         }//if
  111.     }//installDB
  112.  
  113.     public long insertContact (Contact pC){
  114.         int iRet = -1;
  115.  
  116.         SQLiteDatabase db = this.getWritableDatabase();
  117.  
  118.         if (db!=null){
  119.             String strName = pC.getmName();
  120.             String strNumber = pC.getmNumber();
  121.  
  122.             ContentValues pairsCV = new ContentValues();
  123.             pairsCV.put(COL_NAME, strName);
  124.             pairsCV.put(COL_NUMBER, strNumber);
  125.  
  126.             //-1 on insert failure
  127.             long idOfTheNewlyInsertedRecord = db.insert(
  128.                 TABLE_CONTACTS,
  129.                 null,
  130.                 pairsCV
  131.             );
  132.  
  133.             db.close();
  134.             return idOfTheNewlyInsertedRecord;
  135.  
  136.         }//if
  137.  
  138.         return iRet;
  139.     }//insertContact
  140.  
  141.     public final static String SQL_SELECT_BY_NAME_ASC =
  142.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" ASC;";
  143.     public final static String SQL_SELECT_BY_NAME_DESC =
  144.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" DESC;";
  145.  
  146.     public final static String SQL_SELECT_BY_NUMBER_ASC =
  147.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" ASC;";
  148.     public final static String SQL_SELECT_BY_NUMBER_DESC =
  149.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" DESC;";
  150.  
  151.     public final static String SQL_SELECT_BY_ID_ASC =
  152.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" ASC;";
  153.     public final static String SQL_SELECT_BY_ID_DESC =
  154.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" DESC;";
  155.  
  156.     public ArrayList<Contact> selectAllByNameASC(){
  157.         return selectByQuery(SQL_SELECT_BY_NAME_ASC);
  158.     }//selectAllByNameASC
  159.  
  160.     public ArrayList<Contact> selectAllByNameDESC(){
  161.         return selectByQuery(SQL_SELECT_BY_NAME_DESC);
  162.     }//selectAllByNameDESC
  163.  
  164.     public ArrayList<Contact> selectAllByNumberASC(){
  165.         return selectByQuery(SQL_SELECT_BY_NUMBER_ASC);
  166.     }//selectAllByNumberASC
  167.  
  168.     public ArrayList<Contact> selectAllByNumberDESC(){
  169.         return selectByQuery(SQL_SELECT_BY_NUMBER_DESC);
  170.     }//selectAllByNumberDESC
  171.  
  172.     public ArrayList<Contact> selectAllByInsertDateASC(){
  173.         return selectByQuery(SQL_SELECT_BY_ID_ASC);
  174.     }//selectAllByInsertDateASC
  175.  
  176.     public ArrayList<Contact> selectAllByInsertDateDESC(){
  177.         return selectByQuery(SQL_SELECT_BY_ID_DESC);
  178.     }//selectAllByInsertDateDESC
  179.  
  180.     private ArrayList<Contact> selectByQuery (
  181.         String pStrQuery
  182.     )
  183.     {
  184.         ArrayList<Contact> aRet = new ArrayList<Contact>();
  185.  
  186.         try{
  187.             SQLiteDatabase db = this.getReadableDatabase();
  188.  
  189.             if (db!=null) {
  190.                 Cursor cursor = db.rawQuery(
  191.                     pStrQuery,
  192.                     null
  193.                 );
  194.  
  195.                 if (cursor != null) {
  196.                     cursor.moveToFirst();
  197.                     while (!cursor.isAfterLast()) {
  198.                         int idxOfId = cursor.getColumnIndex(COL_ID); //0
  199.                         int _id = cursor.getInt(idxOfId);
  200.  
  201.                         int idxOfName = cursor.getColumnIndex(COL_NAME);//1
  202.                         String strName = cursor.getString(idxOfName);
  203.  
  204.                         //String strName = cursor.getString(1);
  205.  
  206.                         int idxOfNumber = cursor.getColumnIndex(COL_NUMBER); //2
  207.                         String strNumber = cursor.getString(2);
  208.  
  209.                         Contact c = new Contact(
  210.                                 strName,
  211.                                 strNumber
  212.                         );
  213.  
  214.                         aRet.add(c);
  215.  
  216.                         cursor.moveToNext();
  217.                     }//while there are records to be read and included in the return
  218.  
  219.                     db.close();
  220.                 }//if there is a cursor objet to navigate through the objects that the select returned
  221.             }//if we got a readable database
  222.         }
  223.         catch (Exception e){
  224.             Log.e(
  225.                 getClass().getName(),
  226.                 e.toString()
  227.             );
  228.         }
  229.  
  230.         return aRet;
  231.     }//selectAll
  232. }//ContactDB
  233.  
  234. //**
  235.  
  236. package com.joythis.android.mycaller;
  237.  
  238. public class Contact {
  239.     //private long mId;
  240.     private String mName;
  241.     private String mNumber;
  242.  
  243.     public String getmName() {
  244.         return mName;
  245.     }
  246.  
  247.     public String getmNumber() {
  248.         return mNumber;
  249.     }
  250.  
  251.     public Contact
  252.     (
  253.         String pName,
  254.         String pNumber
  255.     ){
  256.         this.mName = pName;
  257.         this.mNumber = pNumber;
  258.     }//Contact
  259.  
  260.     @Override
  261.     public String toString(){
  262.         String strRet = "";
  263.  
  264.         strRet =
  265.             String.format(
  266.                 "%s : %s",
  267.                 this.mName,
  268.                 this.mNumber
  269.             );
  270.  
  271.         return strRet;
  272.     }//toString
  273. }//Contact
  274.  
  275. //**
  276.  
  277. package com.joythis.android.mycaller;
  278.  
  279. import android.app.Activity;
  280. import android.widget.Toast;
  281.  
  282. public class AmUtil {
  283.     Activity mActivity;
  284.  
  285.     public AmUtil(Activity pA){
  286.         this.mActivity = pA;
  287.     }//AmUtil
  288.  
  289.     public void fb(String pMsg){
  290.         Toast t = Toast.makeText(
  291.             this.mActivity,
  292.             pMsg,
  293.             Toast.LENGTH_LONG
  294.         );
  295.         t.show();
  296.     }//fb
  297. }//AmUtil
  298.  
  299. //**
  300. package com.joythis.android.mycaller;
  301.  
  302. import androidx.appcompat.app.AppCompatActivity;
  303.  
  304. import android.content.Context;
  305. import android.os.Bundle;
  306. import android.view.View;
  307. import android.widget.ArrayAdapter;
  308. import android.widget.Button;
  309. import android.widget.EditText;
  310. import android.widget.ListView;
  311. import android.widget.Toast;
  312.  
  313. import java.util.ArrayList;
  314.  
  315. public class MainActivity extends AppCompatActivity {
  316.  
  317.  
  318.     Button.OnClickListener mBtnClickHandler = new Button.OnClickListener() {
  319.         @Override
  320.         public void onClick(View v) {
  321.             switch(v.getId()){
  322.                 case R.id.idBtnInsertContact:
  323.                     actionInsertContact();
  324.                     break;
  325.             }//switch
  326.         }//onClick
  327.     };//mBtnClickHandler
  328.  
  329.     void actionInsertContact(){
  330.         String strContactName = mEtContactName.getText().toString();
  331.         String strContactNumber = mEtContactNumber.getText().toString();
  332.  
  333.         Contact c = new Contact(
  334.             strContactName,
  335.             strContactNumber
  336.         );
  337.  
  338.         long iWhereItWasInserted = mContactDB.insertContact(c);
  339.  
  340.         /*
  341.         Toast t = Toast.makeText(
  342.             this,
  343.             String.valueOf(iWhereItWasInserted),
  344.             Toast.LENGTH_LONG
  345.         );
  346.         t.show();
  347.          */
  348.         mUtil.fb(
  349.             String.valueOf(iWhereItWasInserted)
  350.         );
  351.  
  352.         syncLvContactsWithDB();
  353.     }//actionInsertContact
  354.  
  355.     //data member to enable access to database operations
  356.     //such as insert and select
  357.     ContactDB mContactDB;
  358.     Context mContext;
  359.  
  360.     AmUtil mUtil;
  361.  
  362.     EditText mEtContactName, mEtContactNumber;
  363.     Button mBtnInsertContact;
  364.     ListView mLvContacts;
  365.     ArrayList<Contact> mAlContacts;
  366.     ArrayAdapter<Contact> mAd;
  367.  
  368.     @Override
  369.     protected void onCreate(Bundle savedInstanceState) {
  370.         super.onCreate(savedInstanceState);
  371.         setContentView(R.layout.insert_contact_rl);
  372.  
  373.         init();
  374.     }//onCreate
  375.  
  376.     void init(){
  377.         mContext = this;
  378.         mUtil = new AmUtil(this);
  379.  
  380.         mEtContactName = findViewById(R.id.idEtContactName);
  381.         mEtContactNumber = findViewById(R.id.idEtContactNumber);
  382.  
  383.         mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
  384.         mBtnInsertContact.setOnClickListener(
  385.             mBtnClickHandler //data abstraction
  386.         );
  387.  
  388.         mLvContacts = findViewById(R.id.idLvContacts);
  389.  
  390.         mAlContacts = new ArrayList<>();
  391.  
  392.         mContactDB = new ContactDB(mContext);
  393.  
  394.         mAd = new ArrayAdapter<>(
  395.             mContext,
  396.             android.R.layout.simple_list_item_1,
  397.             mAlContacts
  398.         );
  399.         mLvContacts.setAdapter(mAd);
  400.  
  401.         syncLvContactsWithDB();
  402.  
  403.         /*
  404.         mContactDB.insertContact
  405.             (new Contact("Artur", "123"));
  406.  
  407.         ArrayList<Contact> all = mContactDB.selectAll();
  408.          */
  409.  
  410.     }//init
  411.  
  412.     void syncLvContactsWithDB(){
  413.         ArrayList<Contact> alTemp =
  414.             mContactDB.selectAllByInsertDateDESC();
  415.         if (alTemp!=null && alTemp.size()>0){
  416.             mAlContacts.clear();
  417.             for (Contact c : alTemp){
  418.                 mAlContacts.add(c);
  419.             }//for
  420.             mAd.notifyDataSetChanged();
  421.         }//if
  422.     }//syncLvContactsWithDB
  423. }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment