Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.mycaller;
- 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;
- public class ContactDB extends SQLiteOpenHelper {
- public final static int CONTACTS_DB_VERSION = 33;
- public final static String CONTACTS_DB_NAME = "CONTACTS.DB";
- 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";
- public final static String DROP_CONTACTS =
- "DROP TABLE IF EXISTS "+TABLE_CONTACTS+";";
- /*
- 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 AUTOINCREMENT,\n"+
- COL_NAME+" TEXT NOT NULL,\n"+
- COL_NUMBER+" TEXT NOT NULL\n"+
- ");\n";
- public ContactDB(
- @Nullable Context context
- )
- {
- super(
- context,
- CONTACTS_DB_NAME,
- null,
- CONTACTS_DB_VERSION
- );
- }//ContactDB
- public ContactDB(
- @Nullable Context context,
- @Nullable String name,
- @Nullable SQLiteDatabase.CursorFactory factory,
- int version
- )
- {
- super(context, name, factory, version);
- }//ContactDB
- /*
- this method will install the necessary
- infrastructure (create the table(s))
- this method will be AUTOMATICALLY called by the
- framework upon the 1st need to read or write data
- in "Contact" related table(s)
- */
- @Override
- public void onCreate(SQLiteDatabase db) {
- installDB(db);
- }//onCreate
- /*
- will be automatically called whenever the database
- version changes
- */
- @Override
- public void onUpgrade
- (SQLiteDatabase db, int oldVersion, int newVersion)
- {
- try {
- if (newVersion > oldVersion) {
- db.execSQL(DROP_CONTACTS);
- installDB(db);
- }
- }
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }
- }//onUpgrade
- void installDB(SQLiteDatabase pDB){
- if (pDB!=null){
- try {
- pDB.execSQL(CREATE_TABLE_CONTACTS);
- }
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }
- }//if
- }//installDB
- public long insertContact (Contact pC){
- int iRet = -1;
- SQLiteDatabase db = this.getWritableDatabase();
- if (db!=null){
- String strName = pC.getmName();
- String strNumber = pC.getmNumber();
- ContentValues pairsCV = new ContentValues();
- pairsCV.put(COL_NAME, strName);
- pairsCV.put(COL_NUMBER, strNumber);
- //-1 on insert failure
- long idOfTheNewlyInsertedRecord = db.insert(
- TABLE_CONTACTS,
- null,
- pairsCV
- );
- db.close();
- return idOfTheNewlyInsertedRecord;
- }//if
- return iRet;
- }//insertContact
- public final static String SQL_SELECT_BY_NAME_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" ASC;";
- public final static String SQL_SELECT_BY_NAME_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" DESC;";
- public final static String SQL_SELECT_BY_NUMBER_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" ASC;";
- public final static String SQL_SELECT_BY_NUMBER_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" DESC;";
- public final static String SQL_SELECT_BY_ID_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" ASC;";
- public final static String SQL_SELECT_BY_ID_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" DESC;";
- public ArrayList<Contact> selectAllByNameASC(){
- return selectByQuery(SQL_SELECT_BY_NAME_ASC);
- }//selectAllByNameASC
- public ArrayList<Contact> selectAllByNameDESC(){
- return selectByQuery(SQL_SELECT_BY_NAME_DESC);
- }//selectAllByNameDESC
- public ArrayList<Contact> selectAllByNumberASC(){
- return selectByQuery(SQL_SELECT_BY_NUMBER_ASC);
- }//selectAllByNumberASC
- public ArrayList<Contact> selectAllByNumberDESC(){
- return selectByQuery(SQL_SELECT_BY_NUMBER_DESC);
- }//selectAllByNumberDESC
- public ArrayList<Contact> selectAllByInsertDateASC(){
- return selectByQuery(SQL_SELECT_BY_ID_ASC);
- }//selectAllByInsertDateASC
- public ArrayList<Contact> selectAllByInsertDateDESC(){
- return selectByQuery(SQL_SELECT_BY_ID_DESC);
- }//selectAllByInsertDateDESC
- private ArrayList<Contact> selectByQuery (
- String pStrQuery
- )
- {
- ArrayList<Contact> aRet = new ArrayList<Contact>();
- try{
- SQLiteDatabase db = this.getReadableDatabase();
- if (db!=null) {
- Cursor cursor = db.rawQuery(
- pStrQuery,
- null
- );
- if (cursor != null) {
- cursor.moveToFirst();
- while (!cursor.isAfterLast()) {
- int idxOfId = cursor.getColumnIndex(COL_ID); //0
- int _id = cursor.getInt(idxOfId);
- int idxOfName = cursor.getColumnIndex(COL_NAME);//1
- String strName = cursor.getString(idxOfName);
- //String strName = cursor.getString(1);
- int idxOfNumber = cursor.getColumnIndex(COL_NUMBER); //2
- String strNumber = cursor.getString(2);
- Contact c = new Contact(
- strName,
- strNumber
- );
- aRet.add(c);
- cursor.moveToNext();
- }//while there are records to be read and included in the return
- db.close();
- }//if there is a cursor objet to navigate through the objects that the select returned
- }//if we got a readable database
- }
- catch (Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }
- return aRet;
- }//selectAll
- }//ContactDB
- //**
- package com.joythis.android.mycaller;
- public class Contact {
- //private long mId;
- private String mName;
- private String mNumber;
- public String getmName() {
- return mName;
- }
- public String getmNumber() {
- return mNumber;
- }
- public Contact
- (
- String pName,
- String pNumber
- ){
- this.mName = pName;
- this.mNumber = pNumber;
- }//Contact
- @Override
- public String toString(){
- String strRet = "";
- strRet =
- String.format(
- "%s : %s",
- this.mName,
- this.mNumber
- );
- return strRet;
- }//toString
- }//Contact
- //**
- package com.joythis.android.mycaller;
- import android.app.Activity;
- import android.widget.Toast;
- public class AmUtil {
- Activity mActivity;
- public AmUtil(Activity pA){
- this.mActivity = pA;
- }//AmUtil
- public void fb(String pMsg){
- Toast t = Toast.makeText(
- this.mActivity,
- pMsg,
- Toast.LENGTH_LONG
- );
- t.show();
- }//fb
- }//AmUtil
- //**
- package com.joythis.android.mycaller;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- Button.OnClickListener mBtnClickHandler = new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnInsertContact:
- actionInsertContact();
- break;
- }//switch
- }//onClick
- };//mBtnClickHandler
- void actionInsertContact(){
- String strContactName = mEtContactName.getText().toString();
- String strContactNumber = mEtContactNumber.getText().toString();
- Contact c = new Contact(
- strContactName,
- strContactNumber
- );
- long iWhereItWasInserted = mContactDB.insertContact(c);
- /*
- Toast t = Toast.makeText(
- this,
- String.valueOf(iWhereItWasInserted),
- Toast.LENGTH_LONG
- );
- t.show();
- */
- mUtil.fb(
- String.valueOf(iWhereItWasInserted)
- );
- syncLvContactsWithDB();
- }//actionInsertContact
- //data member to enable access to database operations
- //such as insert and select
- ContactDB mContactDB;
- Context mContext;
- AmUtil mUtil;
- EditText mEtContactName, mEtContactNumber;
- Button mBtnInsertContact;
- ListView mLvContacts;
- ArrayList<Contact> mAlContacts;
- ArrayAdapter<Contact> mAd;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.insert_contact_rl);
- init();
- }//onCreate
- void init(){
- mContext = this;
- mUtil = new AmUtil(this);
- mEtContactName = findViewById(R.id.idEtContactName);
- mEtContactNumber = findViewById(R.id.idEtContactNumber);
- mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
- mBtnInsertContact.setOnClickListener(
- mBtnClickHandler //data abstraction
- );
- mLvContacts = findViewById(R.id.idLvContacts);
- mAlContacts = new ArrayList<>();
- mContactDB = new ContactDB(mContext);
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlContacts
- );
- mLvContacts.setAdapter(mAd);
- syncLvContactsWithDB();
- /*
- mContactDB.insertContact
- (new Contact("Artur", "123"));
- ArrayList<Contact> all = mContactDB.selectAll();
- */
- }//init
- void syncLvContactsWithDB(){
- ArrayList<Contact> alTemp =
- mContactDB.selectAllByInsertDateDESC();
- if (alTemp!=null && alTemp.size()>0){
- mAlContacts.clear();
- for (Contact c : alTemp){
- mAlContacts.add(c);
- }//for
- mAd.notifyDataSetChanged();
- }//if
- }//syncLvContactsWithDB
- }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment