Android Question Write Data from ArrayList into EditText field or table

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello Forum,

I have the following problem:
With ContactUtils I receive several contacts phone numbers.
Sometimes it's just 1 phone number, sometimes there are 5 phone numbers and sometimes I there is no phone number.

Now I want to write this phone numbers in up to 4 fields (e.g .: EditText or in a table).
The first difficulty is already now, I get the phone number as ArrayList in this format [Phone Type = Business, Number = + 49-7171-XXXXXX, IsInitialized = true] and in the log file all 4 telephone numbers are shown, but how can I now write them into 4 separate EditText fields (see example)?
B4X:
For Each phone As cuPhone In ContactsUtils1.GetPhones(Kontakt.Id)
        Dim Kontakte_Telefon_1,  Kontakte_Telefon_2, Kontakte_Telefon_3, Kontakte_Telefon_4 As String
        Kontakte_Telefon_1 = ContactsUtils1.GetPhones(Kontakt.Id).Get(0)
        Kontakte_Telefon_2 = ContactsUtils1.GetPhones(Kontakt.Id).Get(1)
        Kontakte_Telefon_3 = ContactsUtils1.GetPhones(Kontakt.Id).Get(2)
        Kontakte_Telefon_4 = ContactsUtils1.GetPhones(Kontakt.Id).Get(3)
            Log(""&phone.Number&"")
Next
 

DonManfred

Expert
Licensed User
Longtime User
Have you tried to use the list directly?
B4X:
dim l as list = ContactsUtils1.GetPhones(Kontakt.Id)
' now iterate through the list to get a phone object...
' add the first phone.Number to textfield1, the second to Tectfield2 and so on
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Have you tried to use the list directly?
B4X:
dim l as list = ContactsUtils1.GetPhones(Kontakt.Id)
' now iterate through the list to get a phone object...
' add the first phone.Number to textfield1, the second to Tectfield2 and so on
Hello Don Manfred,
thank you for your help!

Yes I tried to get the phone numbers directly from the list with this code:
B4X:
Dim allNumbers As List = ContactsUtils1.GetPhones(Kontakt.Id)
For Each phoneNumber As cuPhone In allNumbers
    Kontakte_Telefon_1 = phoneNumber.Number
        Log(Kontakte_Telefon_1)
Next

The result is, that I get the first phone number correctly, but not more than one phone number.
In the log file I get all 4 numbers.
My problem is ... how to iterate through the list, to get all 4 numbers, because in this code there is no Getter.
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Sorry, there is an error message, usig your code:
Compiling code. Error
Error compiling program.
Error description: Unknown member: number
Occurred on line: 3186
Log($"${i}: ${ph.number}"$)
Word: number

Should it be ...
Dim allNumbers As List = ContactsUtils1.GetPhones(Kontakt.Id)
For i = 0 To allNumbers.size-1
Dim ph As cuPhone = allNumbers.get(i)
Log($"${i}: ${ph.number}"$)
Next
 
Last edited:
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
... yes ... but my Problem is how to get out of

Log($"${i}: ${ph.number}"$)

this result:

--> Pseudocode:
Kontakte_Telefon_1 = cu.GetPhones(Id).Get(0)
Kontakte_Telefon_2 = cu.GetPhones(Id).Get(1)
Kontakte_Telefon_3 = cu.GetPhones(Id).Get(2)
Kontakte_Telefon_4 = cu.GetPhones(Id).Get(3)
This is not possible.

I need someting like --> Pseudocode ...
Kontakte_Telefon_($"${i}= ${ph.number}"$)
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Erel, I don't have another choice, because I receive the data as ArrayList from ContactUtils in this format:
(ArrayList) [[PhoneType=null, Number=+49-7171-908-XXXX, IsInitialized=true
], [PhoneType=Geschäft, Number=+49-7171-931-XXXX, IsInitialized=true
], [PhoneType=null, Number=+49-1525-365-XXXX, IsInitialized=true
], [PhoneType=Mobil, Number=+49-1525-365-XXXX, IsInitialized=true
]]
So what do you mean with ... ?
Don't use 4 variables. Use a single list or array.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Instead od using Kontakte_Telefon_1, Kontakte_Telefon_2, Kontakte_Telefon_3 and Kontakte_Telefon_4
Use
Private Kontakte_Telefon(3) As String
And Kontakte_Telefon(0), Kontakte_Telefon(1), Kontakte_Telefon(2) and Kontakte_Telefon(3)
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello Klaus,
you pointed me to the right direction! Thank you very much! The solution is ...
B4X:
Dim allNumbers As List = ContactsUtils1.GetPhones(Kontakt.Id)
    Private Kontakte_Telefon(allNumbers.size) As String
    For i = 0 To allNumbers.size -1
        Dim ph As cuPhone = allNumbers.get(i)
        Kontakte_Telefon(i) = ph.Number
    Next
The next problem is, that it works only with contacts, which have exactly 4 phone numbers ... because the Database wants to receive 4 fields ... but I'm sure I will find a solution like "If Kontakte_Telefon(1) = "" Then ...

Greetings to Switzerland!
 
Upvote 0
Top