Android Example ContactSearcher - Combines ContactsUtils with SearchView

This example uses SearchView to allow the user to search for any contact. The user can enter a substring of the contact name or one of his phone numbers.

This is a modified version of SearchView. Unlike the standard SearchView this version allows us to use different text for searching. The item itself can be any type of object.

Note that the phone number "search text" is stripped from any non-digits characters. In this implementation the query text (the user input) is not stripped, the user is expected to enter the digits only (or the contact's name).
 

Attachments

  • ContactSearcher.zip
    11.1 KB · Views: 3,215

bsnqt

Active Member
Licensed User
Longtime User
Excellent example with SearchView and ContactUtils, thanks Erel... I will have to learn a lot from this.

Just a quick question... In your example, once the list of Contacts is built, you can re-use the index but in reality the Contacts list is something "dynamic" (user can edit, modify individual contact at any time...) so we need to rebuild the index every time we need to search? It is okay but in my case if I do a call blocker APK, the index rebuilding process will take some times ... while the blocker should act very quickly.

Appreciate your help.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You do not need to rebuild the index for every search. In this implementation the index is stored in memory, which means that it will be kept as long as the process lives. Probably no more than a hour or two.
Do not build the index every time that Activity_Create is called. It will be called every time that the user changes the orientation.

You can track the index time and recreate it after x hours / minutes.
 

bsnqt

Active Member
Licensed User
Longtime User
Thank you very much Erel. Let me play a little bit with the example and will revert later if any question. :)
 

bsnqt

Active Member
Licensed User
Longtime User
Dear Erel,

Please don't wrongly think that I want to compare things together :). I just simply want and am trying to learn B4A from you, and here I believe I missed something important. I want to understand better the background of ContactUtils and ContentResolver.

Attached is my demo app where you can load the contacts list to a listview by (1) using ContactUtils library (button1); or (2) by using an Eclipse-built Java library (button2). The codes from where I made the library is given below, I also attached the library as well. If you run the demo, you can see that the loading speed using button 2 (Java library) is much faster than using ContactUtils.

1) Why we still load the Contacts very slowly, using the ContactUtils Library (in your sample app, I can see it loads very quickly). What is wrong with my B4A codes?

2) After loading, the ContactUtils give me only 706 contacts, while the Java library gave me 738 contacts, why it is so different?

Thank you very much. Please help me.

B4X:
public List GetAllContacts(){
        ArrayList<String> al = new ArrayList<String>();
        List list = new List();
        list.Initialize();
        String key;
    
        Cursor phones =  BA.applicationContext.getApplicationContext().getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
        while (phones.moveToNext()) {
            String Name = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String Number = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            key = Name + "\n" + Number;
                  
            al.add(key);
        }
        phones.close();
        list.setObject((java.util.ArrayList)al);
        return list;
    
    }
 

Attachments

  • FindContactsDemo.zip
    10.3 KB · Views: 947
  • FindContacts.zip
    3.2 KB · Views: 963
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User

bsnqt

Active Member
Licensed User
Longtime User
@thedesolatesould: You are correct, that is very true. Thank you very much for the link and your comment.

My 1st question, however, was just how to simply retrieve the display name and the phone number, so I wonder how to optimize my code in B4A to get it loaded faster to achieve my need. Surely if we want to get photo, email then things are differently (may need more time to load)...

Second question was related to the fact that why the number of contacts retrieved is different from the 2 methods, while I expect it would be the same. I think it may be related to the variables when we make the contacts query (Exact As Boolean, VisibleOnly As Boolean...etc in ContactUtils Class).

Best regards
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can easily convert this Java code to B4A with the ContentResolver library. The difference in the number is most probably because you are not fetching the contacts from the main table and therefore it includes duplicates (multiple contacts that are joined).

Getting ALL the phone numbers of each contact requires an additional query. If you omit the call to GetPhones then it will be much faster.

Edit: This question in not related to this thread topic. For further discussions (if needed) please start a new thread.
 
Last edited:

bsnqt

Active Member
Licensed User
Longtime User
Hi Erel, thank you very much. After playing with this ContactUtils plus your guide, I understand many things. Thanks so much.
 

Silver Chi

New Member
Hi..

sorry for my stupid question, i'm a newbie for android,
i try to learn your example code, but when i try to run it, the compiler show this error :

"
Parsing code. Error
Error parsing program.
Error description: Unknown type: contentresolver
Are you missing a library reference?
Occurred on line: 8
Private cr As ContentResolver
"

what should i do to fix it? thank you..
 

derez

Expert
Licensed User
Longtime User
Erel
The example runs fine on lower versions of android, but on 4.4.2 nexus 5 it just does not install without any error report.
 

derez

Expert
Licensed User
Longtime User
I'm using bridge on wifi, legacy debug.
** Activity (main) Resume **
Setting install_non_market_apps has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
** Service (service1) Destroy **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
GC_CONCURRENT freed 240K, 3% free 17359K/17744K, paused 2ms+3ms, total 39ms
Installing file.
** Activity (main) Pause, UserClosed = false **
Installing file.
GC_CONCURRENT freed 683K, 5% free 17067K/17900K, paused 9ms+1ms, total 46ms
WAIT_FOR_CONCURRENT_GC blocked 23ms
** Activity (main) Resume **
Setting install_non_market_apps has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
Installing file.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Setting install_non_market_apps has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
Installing file.
GC_CONCURRENT freed 388K, 5% free 17065K/17900K, paused 3ms+2ms, total 12ms
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Setting install_non_market_apps has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
** Activity (main) Pause, UserClosed = true **
 

air cover

Member
Licensed User
Longtime User
I used a slightly different approach, but it is rather fast executing. Erel's earlier contactutils example loaded your phone contacts into a listview (here named ContactListView). I added an EditText box onto the layout named SearchContacts.

Then this simple subroutine allowed near instant contact searches:
B4X:
Sub SearchContacts_TextChanged (Old As String, New As String)
Dim temp As String
Dim allContacts As List = cu.FindAllContacts(True)

allContacts.SortType("DisplayName", True )
ContactListview.Clear

    For Each c As cuContact In cu.FindAllContacts(True) 'Load phone contacts into listbox
        temp=c.DisplayName
        If temp.Contains(New) OR New="" Then
            ContactListview.AddSingleLine2(c.DisplayName, c)
        End If
    Next
End Sub
 

AlbertMHaim

New Member
Licensed User
Longtime User
This example uses SearchView to allow the user to search for any contact. The user can enter a substring of the contact name or one of his phone numbers.

This is a modified version of SearchView. Unlike the standard SearchView this version allows us to use different text for searching. The item itself can be any type of object.

Note that the phone number "search text" is stripped from any non-digits characters. In this implementation the query text (the user input) is not stripped, the user is expected to enter the digits only (or the contact's name).


What is the code to view it fullscreen? It only uses half of the screen?
 
Top