Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.myphonecaller;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- 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 members
- Context mContext;
- ContactDB mDB;
- AmUtil mUtil;
- ArrayList<Contact> mAlContacts; //runtime representation of the contacts managed by the app
- EditText mEtName, mEtNumber;
- Button mBtnInsertContact;
- ListView mLvContacts;
- ArrayAdapter<Contact> mAd;
- Button.OnClickListener mButtonClickHandler =
- new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnInsertContact:
- actionInsertContact();
- break;
- }//switch
- }//onClick
- };//mButtonClickHandler
- void actionInsertContact(){
- String strName = mEtName.getText().toString().trim();
- String strNumber = mEtNumber.getText().toString().trim();
- boolean bCheck = strName.length()>0 && strNumber.length()>0;
- if (bCheck){
- Contact c = new Contact(strName, strNumber);
- mDB.insertContact(c);
- //mAlContacts.add(c);
- syncContactsDataWithTheirPresentation();
- }//
- else{
- mUtil.fb("Name and Number can NOT be empty!");
- }
- }//actionInsertContact
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.insert_contact_rl);
- init();
- }//onCreate
- void init(){
- mContext = this;
- mDB = new ContactDB(this);
- mUtil = new AmUtil(this);
- mAlContacts = new ArrayList<>();
- mEtName = findViewById(R.id.idEtName);
- mEtNumber = findViewById(R.id.idEtNumber);
- mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
- mLvContacts = findViewById(R.id.idLvContacts);
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlContacts
- );
- mLvContacts.setAdapter(mAd);
- //TODO:
- /*
- layout
- op layout
- */
- mAlContacts = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation();
- }
- /*
- this method assigns a "menu resource file" to the
- activity's action bar.
- */
- @Override
- public boolean onCreateOptionsMenu(Menu pMenu) {
- MenuInflater minf = this.getMenuInflater();
- if (minf!=null){
- minf.inflate(
- R.menu.sorting_options_menu,//id the XML for the menu
- pMenu //Java ref to assign to the menu
- );
- }//if
- return super.onCreateOptionsMenu(pMenu);
- }//onCreateOptionsMenu
- /*
- here we code what should happen upon the selection of
- any of the menu options
- */
- @Override
- public boolean onOptionsItemSelected(@NonNull MenuItem pMenuItem) {
- switch(pMenuItem.getItemId()){
- case R.id.idMenuItemSortNameAsc:
- actionSortNameAsc();
- break;
- case R.id.idMenuItemSortNameDesc:
- actionSortNameDesc();
- break;
- case R.id.idMenuItemSortNumberAsc:
- actionSortNumberAsc();
- break;
- case R.id.idMenuItemSortNumberDesc:
- actionSortNumberDesc();
- break;
- case R.id.idMenuItemSortInsertDateAsc:
- actionSortInsertDateAsc();
- break;
- case R.id.idMenuItemSortInsertDateDesc:
- actionSortInsertDateDesc();
- break;
- }//switch
- return super.onOptionsItemSelected(pMenuItem);
- }
- void actionSortNameAsc(){
- mAlContacts = mDB.selectAllContactsByNameASC();
- syncContactsDataWithTheirPresentation();
- }
- void actionSortNameDesc(){
- mAlContacts = mDB.selectAllContactsByNameDESC();
- syncContactsDataWithTheirPresentation();
- }
- void actionSortNumberAsc(){
- mAlContacts = mDB.selectAllContactsByNumberASC();
- syncContactsDataWithTheirPresentation();
- }
- void actionSortNumberDesc(){
- mAlContacts = mDB.selectAllContactsByNumberDESC();
- syncContactsDataWithTheirPresentation();
- }
- void actionSortInsertDateAsc(){
- mAlContacts = mDB.selectAllContactsByInsertDateASC();
- syncContactsDataWithTheirPresentation();
- }
- void actionSortInsertDateDesc(){
- mAlContacts = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation();
- }
- void syncContactsDataWithTheirPresentation(){
- //TODO, it depends on our options for the layout design
- ArrayList<Contact> temp = mDB.selectAllContactsByInsertDateDESC();
- if (temp!=null && temp.size()>0){
- mAlContacts.clear();
- for (Contact c: temp){
- mAlContacts.add(c);
- }//for
- }//if
- mAd.notifyDataSetChanged();
- }
- }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment