Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.myphonecaller;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.util.Log;
- import android.widget.Toast;
- import androidx.core.content.ContextCompat;
- import java.net.URI;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class AmUtil {
- Activity mActivity;
- public AmUtil(Activity pA){
- this.mActivity = pA;
- }//AmUtil
- public void fb(
- String pStrMsg
- ){
- Toast t = Toast.makeText(
- this.mActivity,
- pStrMsg,
- Toast.LENGTH_LONG
- );
- t.show();
- }//fb
- public void phoneTo(
- String pStrPhoneNumber
- ){
- try {
- Uri uriForPhoneNumber = Uri.parse("tel:" + pStrPhoneNumber);
- Intent intentForMakingThePhoneCall =
- new Intent(
- Intent.ACTION_CALL
- );
- intentForMakingThePhoneCall.setData(
- uriForPhoneNumber
- );
- boolean bUserAuthorizedTheAppToMakePhoneCalls =
- ContextCompat.checkSelfPermission(
- mActivity,
- Manifest.permission.CALL_PHONE
- ) == PackageManager.PERMISSION_GRANTED;
- if (bUserAuthorizedTheAppToMakePhoneCalls) {
- this.mActivity.startActivity(
- intentForMakingThePhoneCall
- );
- }
- else{
- boolean bUserAcceptsExplanationsOnWhyThePermissionIsImportant =
- mActivity.shouldShowRequestPermissionRationale(
- Manifest.permission.CALL_PHONE
- );
- if (bUserAcceptsExplanationsOnWhyThePermissionIsImportant){
- fb("Without this permission, the app can NOT make phone calls.");
- }
- }
- }//try
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- }//phoneTo
- public Map<Integer, ArrayList<String>>
- identifyPermissionsGrantedAndDenied(
- String[] paNecessaryPermissions
- )
- {
- Map<Integer, ArrayList<String>> retMap = new HashMap<>();
- ArrayList<String> alGranted = new ArrayList<>();
- ArrayList<String> alDenied = new ArrayList<>();
- for (String permission : paNecessaryPermissions){
- boolean bGranted =
- ContextCompat.checkSelfPermission(
- mActivity,
- permission
- ) == PackageManager.PERMISSION_GRANTED;
- boolean bDenied =
- ContextCompat.checkSelfPermission(
- mActivity,
- permission
- ) == PackageManager.PERMISSION_DENIED;
- if (bGranted) alGranted.add(permission);
- if (bDenied) alDenied.add(permission);
- }//for
- retMap.put(/* Integer 0 */ PackageManager.PERMISSION_GRANTED, alGranted);
- retMap.put(/* Integer -1 */ PackageManager.PERMISSION_DENIED, alDenied);
- return retMap;
- }//identifyPermissionsGrantedAndDenied
- public void requestNecessaryPermissionsNotYetGranted(
- String[] paNecessaryPermissions,
- int piCallBackCodeForWhenTheUserResponds
- ){
- /*
- do not request permissions already granted
- */
- Map<Integer, ArrayList<String>> map =
- this.identifyPermissionsGrantedAndDenied
- (paNecessaryPermissions);
- ArrayList<String> alDenied = map.get(PackageManager.PERMISSION_DENIED);
- if(alDenied.size()>0){
- //convert from ArrayList<String> to String[]
- String[] aDenied = new String[alDenied.size()];
- alDenied.toArray(aDenied);
- this.mActivity.requestPermissions(
- //alDenied, //invalid syntax , because String[] is expected
- aDenied,
- piCallBackCodeForWhenTheUserResponds
- );
- }//if
- }//requestNecessaryPermissionsNotYetGranted
- /*
- receives a list of necessary permissions
- returns a String which states the status of each necessary permission
- */
- public String permissionsStatusToString(
- String[] paNecessaryPermissions
- ){
- String strRet = "";
- Map<Integer, ArrayList<String>> map =
- this.identifyPermissionsGrantedAndDenied(paNecessaryPermissions);
- ArrayList<String> alGranted =
- map.get(PackageManager.PERMISSION_GRANTED);
- ArrayList<String> alDenied =
- map.get(PackageManager.PERMISSION_DENIED);
- strRet+="GRANTED:\n";
- for(String spg : alGranted) strRet+=spg+"\n";
- strRet+="\n";
- strRet+="DENIED:\n";
- for(String spd : alDenied) strRet+=spd+"\n";
- return strRet;
- }//permissionsStatusToString
- }//AmUtil
- //**
- package com.joythis.android.myphonecaller;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.Manifest;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import java.util.ArrayList;
- import java.util.Map;
- public class MainActivity extends AppCompatActivity {
- //const to represent the necessary runtime permissions
- public final static String[] NECESSARY_PERMISSIONS =
- {
- Manifest.permission.CALL_PHONE
- };
- public final int
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ = 321;
- //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;
- ListView.OnItemLongClickListener mLvLongClickHandler =
- new ListView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick
- (AdapterView<?> parent, //the ListView
- View view, //the particular item with which the user is interacting with
- int position, //the index of the item in the corresponding data
- long id
- )
- {
- Contact c = mAlContacts.get(position);
- String strNumber = c.getNumber();
- mUtil.fb("Will phone to "+strNumber);
- mUtil.phoneTo(strNumber);
- //return false; //the chain of events will keep going on
- return true; //the event was fully consumed
- }//onItemLongClick
- };//mLvLongClickHandler
- 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);
- long iWhereInserted = mDB.insertContact(c);
- //mAlContacts.add(c);
- ArrayList<Contact> alAfterInsert = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alAfterInsert);
- }//
- 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);
- mBtnInsertContact.setOnClickListener(
- mButtonClickHandler
- );
- mLvContacts = findViewById(R.id.idLvContacts);
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1, //the object for viewing each list item
- mAlContacts //the data (at a specific memory address)
- );
- mLvContacts.setAdapter(mAd);
- mLvContacts.setOnItemLongClickListener
- (mLvLongClickHandler);
- //TODO:
- /*
- layout
- op layout
- */
- ArrayList<Contact> alSortedByDateDesc = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alSortedByDateDesc);
- mUtil.requestNecessaryPermissionsNotYetGranted(
- NECESSARY_PERMISSIONS,
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ
- );
- }//init
- /*
- method that will automaticall called when the
- user completes his/her answers regarding the permission(s)
- that were requested
- */
- @Override
- public void onRequestPermissionsResult(
- int requestCode, //identified to whom the answer is destined to
- //paralell arrays
- @NonNull String[] permissions, //e.g. {p1, p2}
- @NonNull int[] grantResults //e.g. {GRANTED, DENIED}
- )
- {
- for (int idx=0; idx<permissions.length; idx++){
- String strCurrentPermission = permissions[idx];
- int iStatus = grantResults[idx];
- boolean bDenied =
- iStatus == PackageManager.PERMISSION_DENIED;
- if (bDenied){
- if (this.shouldShowRequestPermissionRationale(
- strCurrentPermission
- )){
- //explain why it is important NOT to deny the permission
- mUtil.fb(
- "without the"+
- strCurrentPermission +
- " the app will not work as it should."
- );
- }
- }
- }
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- }//onRequestPermissionsResult
- /*
- 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;
- case R.id.idMenuItemCheckPermissions:
- actionCheckPermissions();
- break;
- }//switch
- return super.onOptionsItemSelected(pMenuItem);
- }
- void actionCheckPermissions(){
- String strCurrentPermissionsStatus =
- mUtil.permissionsStatusToString(NECESSARY_PERMISSIONS);
- mUtil.fb(strCurrentPermissionsStatus);
- }//actionCheckPermissions
- void actionSortNameAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNameDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNumberAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNumberDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortInsertDateAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortInsertDateDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void syncContactsDataWithTheirPresentation(
- ArrayList<Contact> pAlContacts
- ){
- //would be wrong!
- //mAlContacts = pAlContacts; //would change the memory address!
- if (pAlContacts!=null && pAlContacts.size()>0){
- mAlContacts.clear(); //clears the data, but keeps the original memory address to which the Adapter was bound
- for (Contact c: pAlContacts){
- mAlContacts.add(c);
- }//for
- }//if
- mAd.notifyDataSetChanged();
- }//syncContactsDataWithTheirPresentation
- }//MainActivity
- //
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_weight="1"
- android:id="@+id/idTvName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:layout_weight="1"
- android:id="@+id/idBtnCall"
- android:drawableLeft="@drawable/ic_baseline_local_phone_24"
- android:drawableRight="@drawable/ic_baseline_local_phone_24"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- //**
- package com.joythis.android.myphonecaller;
- import android.content.Context;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import androidx.annotation.NonNull;
- import java.util.ArrayList;
- import java.util.List;
- public class ContactAdapter extends ArrayAdapter<Contact> {
- Context mContext;
- int mLayoutResource;
- ArrayList<Contact> mContacts;
- Button.OnClickListener mButtonClickHandler;
- public ContactAdapter(
- @NonNull Context context,
- int resource,
- @NonNull ArrayList<Contact> objects,
- Button.OnClickListener pButtonClickHandler
- )
- {
- super(context, resource, objects);
- this.mContext = context;
- this.mLayoutResource = resource;
- this.mContacts = objects;
- this.mButtonClickHandler = pButtonClickHandler;
- }//ContactAdapter
- }//ContactAdapter
- //
- package com.joythis.android.myphonecaller;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.TextView;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import java.util.ArrayList;
- import java.util.List;
- public class ContactAdapter extends ArrayAdapter<Contact> {
- Context mContext;
- int mLayoutResource;
- ArrayList<Contact> mContacts;
- Button.OnClickListener mButtonClickHandler;
- public ContactAdapter(
- @NonNull Context context,
- int resource,
- @NonNull ArrayList<Contact> objects,
- Button.OnClickListener pButtonClickHandler
- )
- {
- super(context, resource, objects);
- this.mContext = context;
- this.mLayoutResource = resource;
- this.mContacts = objects;
- this.mButtonClickHandler = pButtonClickHandler;
- }//ContactAdapter
- @NonNull
- @Override
- public View getView
- (
- int position, //index in mContacts
- @Nullable View convertView, //View that will use the custom layout and that must be returned
- @NonNull ViewGroup parent //container for the View
- )
- {
- LayoutInflater linf =
- (LayoutInflater)
- this.mContext.getSystemService(
- mContext.LAYOUT_INFLATER_SERVICE
- );
- if (linf!=null){
- convertView =
- linf.inflate(
- this.mLayoutResource,
- parent,
- false
- );
- if (convertView!=null){
- TextView tv = convertView.findViewById(R.id.idTvName);
- Button btn = convertView.findViewById(R.id.idBtnCall);
- Contact c = mContacts.get(position);
- String sName = c.getName();
- String sNumber = c.getNumber();
- tv.setText(sName);
- btn.setText(sNumber);
- btn.setOnClickListener(this.mButtonClickHandler);
- return convertView;
- }
- }
- return super.getView(position, convertView, parent);
- }//getView
- }//ContactAdapter
- //
- package com.joythis.android.myphonecaller;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.Manifest;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import java.util.ArrayList;
- import java.util.Map;
- public class MainActivity extends AppCompatActivity {
- //const to represent the necessary runtime permissions
- public final static String[] NECESSARY_PERMISSIONS =
- {
- Manifest.permission.CALL_PHONE
- };
- public final int
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ = 321;
- //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; //"default" adapter, in use until 2020-12-10
- ContactAdapter mAd2; //2020-12-10, custom adapter
- ListView.OnItemLongClickListener mLvLongClickHandler =
- new ListView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick
- (AdapterView<?> parent, //the ListView
- View view, //the particular item with which the user is interacting with
- int position, //the index of the item in the corresponding data
- long id
- )
- {
- Contact c = mAlContacts.get(position);
- String strNumber = c.getNumber();
- mUtil.fb("Will phone to "+strNumber);
- mUtil.phoneTo(strNumber);
- //return false; //the chain of events will keep going on
- return true; //the event was fully consumed
- }//onItemLongClick
- };//mLvLongClickHandler
- 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);
- long iWhereInserted = mDB.insertContact(c);
- //mAlContacts.add(c);
- ArrayList<Contact> alAfterInsert = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alAfterInsert);
- }//
- 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);
- mBtnInsertContact.setOnClickListener(
- mButtonClickHandler
- );
- mLvContacts = findViewById(R.id.idLvContacts);
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1, //the object for viewing each list item
- mAlContacts //the data (at a specific memory address)
- );
- //speedy
- mAd2 = new ContactAdapter(
- mContext,
- R.layout.contact_ll,
- mAlContacts,
- mButtonClickHandler
- );
- //mLvContacts.setAdapter(mAd);
- mLvContacts.setAdapter(mAd2);
- mLvContacts.setOnItemLongClickListener
- (mLvLongClickHandler);
- //TODO:
- /*
- layout
- op layout
- */
- ArrayList<Contact> alSortedByDateDesc = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alSortedByDateDesc);
- mUtil.requestNecessaryPermissionsNotYetGranted(
- NECESSARY_PERMISSIONS,
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ
- );
- }//init
- /*
- method that will automaticall called when the
- user completes his/her answers regarding the permission(s)
- that were requested
- */
- @Override
- public void onRequestPermissionsResult(
- int requestCode, //identified to whom the answer is destined to
- //paralell arrays
- @NonNull String[] permissions, //e.g. {p1, p2}
- @NonNull int[] grantResults //e.g. {GRANTED, DENIED}
- )
- {
- for (int idx=0; idx<permissions.length; idx++){
- String strCurrentPermission = permissions[idx];
- int iStatus = grantResults[idx];
- boolean bDenied =
- iStatus == PackageManager.PERMISSION_DENIED;
- if (bDenied){
- if (this.shouldShowRequestPermissionRationale(
- strCurrentPermission
- )){
- //explain why it is important NOT to deny the permission
- mUtil.fb(
- "without the"+
- strCurrentPermission +
- " the app will not work as it should."
- );
- }
- }
- }
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- }//onRequestPermissionsResult
- /*
- 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.idMenuItemRequestDeniedPerms:
- mUtil.requestNecessaryPermissionsNotYetGranted(
- NECESSARY_PERMISSIONS,
- CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ
- );
- break;
- 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;
- case R.id.idMenuItemCheckPermissions:
- actionCheckPermissions();
- break;
- }//switch
- return super.onOptionsItemSelected(pMenuItem);
- }
- void actionCheckPermissions(){
- String strCurrentPermissionsStatus =
- mUtil.permissionsStatusToString(NECESSARY_PERMISSIONS);
- mUtil.fb(strCurrentPermissionsStatus);
- }//actionCheckPermissions
- void actionSortNameAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNameDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNumberAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNumberDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortInsertDateAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortInsertDateDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void syncContactsDataWithTheirPresentation(
- ArrayList<Contact> pAlContacts
- ){
- //would be wrong!
- //mAlContacts = pAlContacts; //would change the memory address!
- if (pAlContacts!=null && pAlContacts.size()>0){
- mAlContacts.clear(); //clears the data, but keeps the original memory address to which the Adapter was bound
- for (Contact c: pAlContacts){
- mAlContacts.add(c);
- }//for
- }//if
- mAd.notifyDataSetChanged();
- }//syncContactsDataWithTheirPresentation
- }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment