Android Question Compile all contacts in a .txt file

alvin1929

Member
This sample code shows contact names in a list view
B4X:
Sub Process_Globals
    Private cu As ContactsUtils
    Private rp As RuntimePermissions
End Sub

Sub Globals
    Private ImageView1 As B4XView
    Private EditText1 As B4XView
    Private CLV1 As CustomListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        cu.Initialize
    End If
    Activity.LoadLayout("1")
    rp.CheckAndRequest(rp.PERMISSION_WRITE_CONTACTS)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        For Each c As cuContact In cu.FindAllContacts(True)
            CLV1.AddTextItem(c.DisplayName, c)
        Next
    Else
        ToastMessageShow("No permission", True)
    End If
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub CLV1_ItemLongClick (Index As Int, Value As Object)
    Dim c As cuContact = Value
    cu.SetStarred(c.Id, Not(cu.GetStarred(c.Id)))
    'update the text
    CLV1_ItemClick(Index, Value)
End Sub

Sub CLV1_ItemClick (Index As Int, Value As Object)
    Dim c As cuContact = Value
    Dim bmp As Bitmap = cu.GetPhoto(c.Id)
    If bmp.IsInitialized Then ImageView1.SetBitmap(bmp) Else ImageView1.SetBitmap(Null)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(c.DisplayName).Append(CRLF).Append("Note: ").Append(cu.GetNote(c.Id)).Append(CRLF)
    sb.Append("Starred: ").Append(cu.GetStarred(c.Id)).Append(CRLF)
    For Each phone As cuPhone In cu.GetPhones(c.Id)
        sb.Append(phone.Number & ", " & phone.PhoneType).Append(CRLF)
    Next
    For Each email As cuEmail In cu.GetEmails(c.Id)
        sb.Append(email.email).Append(", ").Append(email.EmailType).Append(CRLF)
    Next
    EditText1.Text = sb.ToString
End Sub

would it be possible to compile all contacts together with their number and address as strings and have them inside a .txt file?
 

pluton

Active Member
Licensed User
Longtime User
Did you check this : https://www.b4x.com/android/forum/threads/text-files.6690/

There is :
SaveStringExample
ReadStringExample

WriteListExample
ReadListExample

B4X:
Sub SaveStringExample
    File.WriteString(File.DirRootExternal, "String.txt", _
        "This is some string" & CRLF & "and this is another one.")
End Sub

Sub ReadStringExample
    Msgbox(File.ReadString(File.DirRootExternal, "String.txt"), "")
End Sub

Sub WriteListExample
    Dim List1 As List
    List1.Initialize
    For i = 1 To 100
        List1.Add(i)
    Next
    File.WriteList(File.DirRootExternal, "List.txt", List1)
End Sub

Sub ReadListExample
    Dim List1 As List
    'We are not initializing the list because it just holds the list that returns from File.ReadList
    List1 = File.ReadList(File.DirRootExternal, "List.txt")
    Msgbox("List1.Size = " & List1.Size & CRLF & "The third item is: " & List1.Get(2), "")
End Sub
 
Upvote 0

alvin1929

Member
Did you check this : https://www.b4x.com/android/forum/threads/text-files.6690/

There is :
SaveStringExample
ReadStringExample

WriteListExample
ReadListExample

B4X:
Sub SaveStringExample
    File.WriteString(File.DirRootExternal, "String.txt", _
        "This is some string" & CRLF & "and this is another one.")
End Sub

Sub ReadStringExample
    Msgbox(File.ReadString(File.DirRootExternal, "String.txt"), "")
End Sub

Sub WriteListExample
    Dim List1 As List
    List1.Initialize
    For i = 1 To 100
        List1.Add(i)
    Next
    File.WriteList(File.DirRootExternal, "List.txt", List1)
End Sub

Sub ReadListExample
    Dim List1 As List
    'We are not initializing the list because it just holds the list that returns from File.ReadList
    List1 = File.ReadList(File.DirRootExternal, "List.txt")
    Msgbox("List1.Size = " & List1.Size & CRLF & "The third item is: " & List1.Get(2), "")
End Sub
Thanks! Will be studying this.
 
Upvote 0
Top