am_dot_com

PDM2 2020-11-19

Nov 19th, 2020 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.13 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 = 1;
  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.             }
  89.         }
  90.         catch(Exception e){
  91.             Log.e(
  92.                getClass().getName(),
  93.                e.toString()
  94.             );
  95.         }
  96.  
  97.         installDB(db);
  98.     }//onUpgrade
  99.  
  100.     void installDB(SQLiteDatabase pDB){
  101.         if (pDB!=null){
  102.             try {
  103.                 pDB.execSQL(CREATE_TABLE_CONTACTS);
  104.             }
  105.             catch(Exception e){
  106.                 Log.e(
  107.                     getClass().getName(),
  108.                     e.toString()
  109.                 );
  110.             }
  111.         }//if
  112.     }//installDB
  113.  
  114.     public long insertContact (Contact pC){
  115.         int iRet = -1;
  116.  
  117.         SQLiteDatabase db = this.getWritableDatabase();
  118.  
  119.         if (db!=null){
  120.             String strName = pC.getmName();
  121.             String strNumber = pC.getmNumber();
  122.  
  123.             ContentValues pairsCV = new ContentValues();
  124.             pairsCV.put(COL_NAME, strName);
  125.             pairsCV.put(COL_NUMBER, strNumber);
  126.  
  127.             //-1 on insert failure
  128.             long idOfTheNewlyInsertedRecord = db.insert(
  129.                 TABLE_CONTACTS,
  130.                 null,
  131.                 pairsCV
  132.             );
  133.  
  134.             db.close();
  135.             return idOfTheNewlyInsertedRecord;
  136.  
  137.         }//if
  138.  
  139.         return iRet;
  140.     }//insertContact
  141.    
  142.     public ArrayList<Contact> selectAll (){
  143.         ArrayList<Contact> aRet = new ArrayList<Contact>();
  144.        
  145.         SQLiteDatabase db = this.getReadableDatabase();
  146.        
  147.         if (db!=null){
  148.             //  " ORDER BY "+COL_ID +" DESC" ???
  149.             String strQueryForSelect =
  150.             "SELECT * FROM "+TABLE_CONTACTS+";";
  151.            
  152.             Cursor cursor = db.rawQuery(
  153.                 strQueryForSelect,
  154.                 null
  155.             );
  156.            
  157.             if (cursor!=null){
  158.                 cursor.moveToFirst();
  159.                 while(!cursor.isAfterLast()){
  160.                     int idxOfId = cursor.getColumnIndex(COL_ID); //0
  161.                     int _id = cursor.getInt(idxOfId);
  162.                    
  163.                     int idxOfName = cursor.getColumnIndex(COL_NAME);//1
  164.                     String strName = cursor.getString(idxOfName);
  165.  
  166.                     //String strName = cursor.getString(1);
  167.                    
  168.                     int idxOfNumber = cursor.getColumnIndex(COL_NUMBER); //2
  169.                     String strNumber = cursor.getString(2);
  170.                    
  171.                     Contact c = new Contact(
  172.                         strName,
  173.                         strNumber
  174.                     );
  175.                    
  176.                     aRet.add(c);
  177.                    
  178.                     cursor.moveToNext();
  179.                 }//while there are records to be read and included in the return
  180.             }//if there is a cursor objeto to navigate through the objects that the select returned
  181.         }//if we got a readable database
  182.  
  183.         return aRet;
  184.     }//selectAll
  185. }//ContactDB
  186.  
  187. //**
  188.  
  189. package com.joythis.android.mycaller;
  190.  
  191. public class Contact {
  192.     private String mName;
  193.     private String mNumber;
  194.  
  195.     public String getmName() {
  196.         return mName;
  197.     }
  198.  
  199.     public String getmNumber() {
  200.         return mNumber;
  201.     }
  202.  
  203.     public Contact
  204.     (
  205.         String pName,
  206.         String pNumber
  207.     ){
  208.         this.mName = pName;
  209.         this.mNumber = pNumber;
  210.     }//Contact
  211.  
  212.     public String toString(){
  213.         String strRet = "";
  214.  
  215.         String.format(
  216.             "%s : %s",
  217.             this.mName,
  218.             this.mNumber
  219.         );
  220.  
  221.         return strRet;
  222.     }//toString
  223. }//Contact
  224.  
  225. //**
  226.  
  227. package com.joythis.android.mycaller;
  228.  
  229. import androidx.appcompat.app.AppCompatActivity;
  230.  
  231. import android.content.Context;
  232. import android.os.Bundle;
  233.  
  234. import java.util.ArrayList;
  235.  
  236. public class MainActivity extends AppCompatActivity {
  237.  
  238.     //data member to enable access to database operations
  239.     //such as insert and select
  240.     ContactDB mContactDB;
  241.     Context mContext;
  242.  
  243.     @Override
  244.     protected void onCreate(Bundle savedInstanceState) {
  245.         super.onCreate(savedInstanceState);
  246.         setContentView(R.layout.activity_main);
  247.  
  248.         init();
  249.     }//onCreate
  250.  
  251.     void init(){
  252.         mContactDB = new ContactDB(mContext);
  253.  
  254.         /*
  255.         mContactDB.insertContact(new Contact("Artur", "123"));
  256.         ArrayList<Contact> all = mContactDB.selectAllContacts();
  257.          */
  258.     }//init
  259. }//MainActivity
  260.  
  261. //**
  262.  
  263. <?xml version="1.0" encoding="utf-8"?>
  264. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  265.     android:layout_width="match_parent"
  266.     android:layout_height="match_parent">
  267.  
  268.     <LinearLayout
  269.         android:orientation="horizontal"
  270.         android:id="@+id/LlContact"
  271.         android:layout_width="match_parent"
  272.         android:layout_height="wrap_content">
  273.  
  274.         <EditText
  275.             android:hint="@string/strEtContactNameHint"
  276.             android:id="@+id/idEtContactName"
  277.             android:layout_weight="1"
  278.             android:layout_width="match_parent"
  279.             android:layout_height="wrap_content"/>
  280.  
  281.         <EditText
  282.             android:hint="@string/strEtContactNumberHint"
  283.             android:id="@+id/idEtContactNumber"
  284.             android:layout_weight="1"
  285.             android:layout_width="match_parent"
  286.             android:layout_height="wrap_content"/>
  287.  
  288.     </LinearLayout>
  289.  
  290.     <Button
  291.         android:layout_below="@id/LlContact"
  292.         android:id="@+id/idBtnInsertContact"
  293.         android:text="@string/strBtnInsertContact"
  294.         android:layout_width="match_parent"
  295.         android:layout_height="wrap_content"/>
  296.  
  297.     <ListView
  298.         android:layout_below="@id/idBtnInsertContact"
  299.         android:layout_width="match_parent"
  300.         android:layout_height="wrap_content"/>
  301.  
  302. </RelativeLayout>
  303.  
  304. //**
  305.  
  306. package com.joythis.android.mycaller;
  307.  
  308. import androidx.appcompat.app.AppCompatActivity;
  309.  
  310. import android.content.Context;
  311. import android.os.Bundle;
  312. import android.widget.ArrayAdapter;
  313. import android.widget.Button;
  314. import android.widget.EditText;
  315. import android.widget.ListView;
  316.  
  317. import java.util.ArrayList;
  318.  
  319. public class MainActivity extends AppCompatActivity {
  320.  
  321.     //data member to enable access to database operations
  322.     //such as insert and select
  323.     ContactDB mContactDB;
  324.     Context mContext;
  325.  
  326.     EditText mEtContactName, mEtContactNumber;
  327.     Button mBtnInsertContact;
  328.     ListView mLvContacts;
  329.     ArrayList<Contact> mAlContacts;
  330.     ArrayAdapter<Contact> mAd;
  331.  
  332.     @Override
  333.     protected void onCreate(Bundle savedInstanceState) {
  334.         super.onCreate(savedInstanceState);
  335.         setContentView(R.layout.activity_main);
  336.  
  337.         init();
  338.     }//onCreate
  339.  
  340.     void init(){
  341.         mContext = this;
  342.         mEtContactName = findViewById(R.id.idEtContactName);
  343.         mEtContactNumber = findViewById(R.id.idEtContactNumber);
  344.         mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
  345.         mLvContacts = findViewById(R.id.idLvContacts);
  346.  
  347.         mAlContacts = new ArrayList<>();
  348.  
  349.         mContactDB = new ContactDB(mContext);
  350.         syncLvContactsWithDB();
  351.  
  352.         mAd = new ArrayAdapter<>(
  353.             mContext,
  354.             android.R.layout.simple_list_item_1,
  355.             mAlContacts
  356.         );
  357.         mLvContacts.setAdapter(mAd);
  358.  
  359.         /*
  360.         mContactDB.insertContact(new Contact("Artur", "123"));
  361.         ArrayList<Contact> all = mContactDB.selectAll();
  362.  
  363.          */
  364.     }//init
  365.  
  366.     void syncLvContactsWithDB(){
  367.         ArrayList<Contact> alTemp = mContactDB.selectAll();
  368.         if (alTemp!=null && alTemp.size()>0){
  369.             mAlContacts.clear();
  370.             for (Contact c : alTemp){
  371.                 mAlContacts.add(c);
  372.             }//for
  373.             mAd.notifyDataSetChanged();
  374.         }//if
  375.     }//syncLvContactsWithDB
  376. }
Advertisement
Add Comment
Please, Sign In to add comment