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 = 1;
- 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);
- }
- }
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }
- installDB(db);
- }//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 ArrayList<Contact> selectAll (){
- ArrayList<Contact> aRet = new ArrayList<Contact>();
- SQLiteDatabase db = this.getReadableDatabase();
- if (db!=null){
- // " ORDER BY "+COL_ID +" DESC" ???
- String strQueryForSelect =
- "SELECT * FROM "+TABLE_CONTACTS+";";
- Cursor cursor = db.rawQuery(
- strQueryForSelect,
- 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
- }//if there is a cursor objeto to navigate through the objects that the select returned
- }//if we got a readable database
- return aRet;
- }//selectAll
- }//ContactDB
- //**
- package com.joythis.android.mycaller;
- public class Contact {
- 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
- public String toString(){
- String strRet = "";
- String.format(
- "%s : %s",
- this.mName,
- this.mNumber
- );
- return strRet;
- }//toString
- }//Contact
- //**
- package com.joythis.android.mycaller;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //data member to enable access to database operations
- //such as insert and select
- ContactDB mContactDB;
- Context mContext;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- void init(){
- mContactDB = new ContactDB(mContext);
- /*
- mContactDB.insertContact(new Contact("Artur", "123"));
- ArrayList<Contact> all = mContactDB.selectAllContacts();
- */
- }//init
- }//MainActivity
- //**
- <?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">
- <LinearLayout
- android:orientation="horizontal"
- android:id="@+id/LlContact"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <EditText
- android:hint="@string/strEtContactNameHint"
- android:id="@+id/idEtContactName"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <EditText
- android:hint="@string/strEtContactNumberHint"
- android:id="@+id/idEtContactNumber"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <Button
- android:layout_below="@id/LlContact"
- android:id="@+id/idBtnInsertContact"
- android:text="@string/strBtnInsertContact"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <ListView
- android:layout_below="@id/idBtnInsertContact"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
- //**
- package com.joythis.android.mycaller;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //data member to enable access to database operations
- //such as insert and select
- ContactDB mContactDB;
- Context mContext;
- 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.activity_main);
- init();
- }//onCreate
- void init(){
- mContext = this;
- mEtContactName = findViewById(R.id.idEtContactName);
- mEtContactNumber = findViewById(R.id.idEtContactNumber);
- mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
- mLvContacts = findViewById(R.id.idLvContacts);
- mAlContacts = new ArrayList<>();
- mContactDB = new ContactDB(mContext);
- syncLvContactsWithDB();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlContacts
- );
- mLvContacts.setAdapter(mAd);
- /*
- mContactDB.insertContact(new Contact("Artur", "123"));
- ArrayList<Contact> all = mContactDB.selectAll();
- */
- }//init
- void syncLvContactsWithDB(){
- ArrayList<Contact> alTemp = mContactDB.selectAll();
- if (alTemp!=null && alTemp.size()>0){
- mAlContacts.clear();
- for (Contact c : alTemp){
- mAlContacts.add(c);
- }//for
- mAd.notifyDataSetChanged();
- }//if
- }//syncLvContactsWithDB
- }
Advertisement
Add Comment
Please, Sign In to add comment