Contacts not showing all contacts

anaylor01

Well-Known Member
Licensed User
Longtime User
Is the problem fixed where all contacts are readable? I saw that there was a problem with contacts not added by google not being able to read their info. I tried an example and it seemed to be the case. Does anyone know?
 

optimist

Member
Licensed User
Longtime User
Hallo,
i dont understand exactly your problem, but i attached my way to read all contacts. Feel free to try this. Should work from Android 2 and higher. Suggestions welcome.

B4X:
Sub LogAllContacts

  'to use this example
  '1. copy the attached jar and xml Files in your Library folder 
  '2. activate lib as usual.

  Dim mu As miscUtil
  mu.Initialize
  Dim contacts As Map
  Dim i As Int
  contacts = mu.GetContactMap
  For i = 0 To contacts.Size-1
    Log(contacts.GetValueAt(i))
  Next

End Sub
 

Attachments

  • Lib.zip
    6.4 KB · Views: 269
Last edited:
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I just tried your solution and it does NOT list all contacts from contacts. I am using an Motorola Atrix.
 
Upvote 0

TheDevMan

Member
Licensed User
Longtime User
Optimist,

Would you mind to share that piece of code of the lib as source code?
I want to help you with this make it better than teh orginal one.
 
Upvote 0

Brad

Active Member
Licensed User
Longtime User
My understanding is that you can only access contacts that have been 'starred'
 
Upvote 0

optimist

Member
Licensed User
Longtime User
Optimist,

Would you mind to share that piece of code of the lib as source code?
I want to help you with this make it better than teh orginal one.

this code is straight forward, nothing surprising
noteworthy that managedQuery(person, person_projection, null, null, null) does NOT contain any kind of filter

B4X:
public class miscUtil  {

    private BA m_ba;

   /**
   * Common initialisations
   */   
    public void Initialize(BA ba) {
      m_ba = ba;
    }
    

        /**
   * Erzeut eine Map mit allen vorhandenen Kontakten
   * Key ist eine ID, mit der weitere Kontaktinfos abgefragt werden können 
   */
   public Map GetContactMap() {
      Map personen = new Map();
      personen.Initialize();
   
      Uri person = ContactsContract.Contacts.CONTENT_URI;
      String[] person_projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME
      };
      Cursor person_cur = m_ba.activity.managedQuery(person, person_projection, null, null, null);

      person_cur.moveToFirst();
      while (person_cur.isAfterLast() == false) {
         personen.Put(person_cur.getString(0), person_cur.getString(1));
         person_cur.moveToNext();
      }
      person_cur.close();
      return personen;
   }
}
 
Upvote 0

optimist

Member
Licensed User
Longtime User
I get everytime a nullpointer.

In which line, pointer to which object is null?

I guess you dont call it from an activity or you forgot to call Initialize.
Then the BA would be null.

Another guess is, that you forgot the attributation of the class.
It should be something like
@ShortName("miscUtil") // this is the name as it appear on the IDE
@Permissions(values = {"android.permission.GET_ACCOUNTS", "android.permission.READ_CONTACTS"})
@Author("Michael Peter") // your name
@Version(1.04f) // the version
@ActivityObject

There are two helpfull sticky threads in Libraries developers questions - Android Development Forum - Basic4android
 
Upvote 0

TheDevMan

Member
Licensed User
Longtime User
Hi Optimist,

I changed some code to get also the phonenumbers in a LIST! This make it compatible with B4A.
Im not a real JAVA programmer, but more C & Delphi. So it was a course for me to do something in JAVA.
But i like it. I want to expand the contacts Lib more to make it Cross-Compatible between 1.6 and 2.0

Thnx Optimist!

B4X:
    public List GetContactList(){
       List personenlist = new List();
       personenlist.Initialize();
        
       
      Uri personlist = ContactsContract.Contacts.CONTENT_URI;
      String[] personlist_projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
             ContactsContract.Contacts.HAS_PHONE_NUMBER,
              
      };
      Cursor cur = m_ba.activity.managedQuery(personlist, personlist_projection, null, null, null);
        
      if (cur.getCount() > 0 ) {
         cur.moveToFirst();
         while (cur.isAfterLast() == false) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = m_ba.activity.managedQuery(
                          ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                          null, 
                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                          new String[]{id}, null);
                
                           while (pCur.moveToNext()) {
                              String PhoneNum = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA1));
                              
                              personenlist.Add(name + "=" + PhoneNum );
                              //personenlist.Add(PhoneNum); 
                           } 
                           
                           pCur.close();      
            }       
             
             
            //personen.Put(person_cur.getString(0), person_cur.getString(1));
            cur.moveToNext();
         }
         cur.close();
         
      }
      return personenlist;
             
       
    }
 
Last edited:
Upvote 0
Top