Android Question Trying to add group membership functions to ContactUtils module

Gregg Homan

Member
Licensed User
Longtime User
Hello,

Can someone assist me in getting InitializeGroupTypes() to obtain all the contact groups?

I am trying to add group membership functionality to the ContactUtils module (see code below). cu.GetGroups() works fine as long as the map named groupTypes is properly initialized when cu.Initialize is invoked which is where my problem is. cu.Initialize calls a sub named InitializeGroupTypes which is suppose to obtain all the contact groups, but I can't get it to work properly so I am forced to 'strong-arm' a solution by using a workaround sub namedInitializeGroupTypes2().

The code below calls the new subroutine named cu.GetGroups(c.ID) in order to obtain contacts' membership groups.
B4X:
'Activity Module
Sub Process_Globals
    Dim cu As ContactsUtils            'GGH 01-13-14, requires lib: ContentResolver (version 1.00)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then       
        cu.Initialize
    End If
    For Each c As cuContact In cu.FindAllContacts(True)
        For Each g As cuGroup In cu.GetGroups(c.Id)
            Log(c.DisplayName&"-"&g.GroupID&"-"&g.GroupType)
        Next
    Next   
End Sub

Sub Activity_Resume
End Sub
   
Sub Activity_Pause (UserClosed As Boolean)
End Sub

The code below adds a new type named cuGroup, a new map called groupTypes and two new subroutines named GetGroups() and InitializeGroupTypes() to the ContactUtils module in hopes of adding group membership functionality. The problem is that InitializeGroupTypes() does not properly initialize groupTypes so I am forced to 'strong-arm' a solution by using a workaround sub called InitializeGroupTypes2()
B4X:
'ContactUtils Module
Sub Class_Globals
    ...
    ...
    ...
    Type cuGroup (GroupID As Int, GroupType As String) 
    Private groupTypes As Map 
End Sub

Public Sub Initialize
    ...
    ...
    ...
    InitializeGroupTypes   'Does not work
    'InitializeGroupTypes2 'Workaround sub
End Sub

...
...
...
'This sub works fine as long as groupTypes is properly initialized
Public Sub GetGroups(Id As Long) As List
    Dim res As List
    res.Initialize
    For Each obj() As Object In GetData("vnd.android.cursor.item/group_membership", _
        Array As String("data1", "group_sourceid"), Id, Null)
        Dim g As cuGroup
        g.Initialize
        g.GroupID= obj(0)
        g.GroupType = groupTypes.Get(obj(0))
        res.Add(g)
    Next
    Return res
End Sub

'This sub does not work correctly for the for loop in 
'this sub is never entered and I do not know why
Public Sub InitializeGroupTypes
    groupTypes.Initialize
    Dim crsr As Cursor = cr.Query(dataUri, Array As String("data1","data3"), _
         "mimetype = ?", Array As String("vnd.android.cursor.dir/group"), "")
    For i = 0 To crsr.RowCount-1
        crsr.Position = i
        groupTypes.Put(crsr.GetString2(0),crsr.GetString2(1))
    Next
    crsr.Close
End Sub

'This sub is being used in place of IntializeGroupTypes 
'until I figure out how to get InitializeGroupTypes working
Private Sub InitializeGroupTypes2
    groupTypes.Initialize
    groupTypes.Put("1", "contact")
    groupTypes.Put("2", "starred")
    groupTypes.Put("3", "friends")
    groupTypes.Put("4", "family")
    groupTypes.Put("5", "coworker")
End Sub
...
...
...
 

Gregg Homan

Member
Licensed User
Longtime User
Hello again,

I'll try restating my problem more concisely. I'm trying to access the contact group list using Query within ContentResolver using mimetype = vnd.android.cursor.dir/group or mimetype = vnd.android.cursor.item/group. I suspect these mime types are incorrect for Query() always produces zero records in both code examples below. Can someone suggest a different mime type and/or using Query differently to obtain list of contact groups?

Thanks,
Gregg

B4X:
Private cr As ContentResolver
Dim crsr As Cursor = cr.Query(dataUri, ArrayAsString("data1","data3"), _"mimetype = ?", ArrayAsString("vnd.android.cursor.dir/group"), "")
For i = 0 To crsr.RowCount-1
     'Do something
Next

B4X:
Private cr As ContentResolver
Dim crsr As Cursor = cr.Query(dataUri, ArrayAsString("data1","data3"), _"mimetype = ?", ArrayAsString("vnd.android.cursor.item/group"), "")
For i = 0 To crsr.RowCount-1
     'Do something
Next
 
Upvote 0

Gregg Homan

Member
Licensed User
Longtime User
I forgot to explain dataUri in above code examples.
B4X:
Private dataUri As Uri
dataUri.Parse("content://com.android.contacts/data")
 
Upvote 0

Gregg Homan

Member
Licensed User
Longtime User
Thanks Desolate Soul,

I missed your post when searching the forum regarding the contact group membership problem. The four subroutines involving contact group membership that you added to contactutils were 'spot-on' and greatly appreciated!!! I am curious, is there an established process that Eriel and/or this forum uses to continuously incorporate module/library improvements like yours?

Thanks,
Gregg
 
Upvote 0
Top