Android Example ContactSearcher - Combines ContactsUtils with SearchView

Discussion in 'Tutorials & Examples' started by Erel, Jul 29, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed 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).
     

    Attached Files:

    Kope, ocalle, Azam Rafique and 11 others like this.
  2. HCAZ

    HCAZ Member Licensed User

    Great work, thanks Erel!
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    @HCAZ thank you :) Note that to keep the forum more "clean" it is better to use the 'Like' link (right side of each post). In the future it will also help the search engine.
     
  4. bsnqt

    bsnqt Active Member Licensed 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.
     
    alimanam3386 likes this.
  5. Erel

    Erel Administrator Staff Member Licensed 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.
     
    alimanam3386 likes this.
  6. bsnqt

    bsnqt Active Member Licensed User

    Thank you very much Erel. Let me play a little bit with the example and will revert later if any question. :)
     
  7. bsnqt

    bsnqt Active Member Licensed 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.

    Code:
    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;
        
        }
     

    Attached Files:

    Last edited: Jul 31, 2013
  8. thedesolatesoul

    thedesolatesoul Expert Licensed User

  9. bsnqt

    bsnqt Active Member Licensed 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
     
  10. Erel

    Erel Administrator Staff Member Licensed 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: Jul 31, 2013
  11. bsnqt

    bsnqt Active Member Licensed User

    Hi Erel, thank you very much. After playing with this ContactUtils plus your guide, I understand many things. Thanks so much.
     
  12. Silver Chi

    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..
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    koaunglay likes this.
  14. derez

    derez Expert Licensed 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.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Works here with Nexus 4 running Android 4.4.2. If it doesn't install at all then the problem is somewhere else.

    How are you connected to the device? Have you checked the unfiltered logs?
     
  16. derez

    derez Expert Licensed User

    I'm using bridge on wifi, legacy debug.
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    This issue is not related to ContactSearcher.

    Make sure that there isn't a previous app with the same package name and different sign key.
     
  18. derez

    derez Expert Licensed User

    I changed package name and it installs :)
     
  19. air cover

    air cover Member Licensed 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:
    Code:
    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
     
  20. AlbertMHaim

    AlbertMHaim New Member Licensed User


    What is the code to view it fullscreen? It only uses half of the screen?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice