Android Question Convert the name of an object into the object itself

DanteS

Member
Licensed User
I have some spinners named co_city, co_customers, co_seller, etc, and a rutine Fill_co(co as spinner, data as list) to fill the spinners with the members of a list.

To fill the list I use the PostString method to execute a query and receive the list from the database in the JobDone rutine. I also store the name of the spinner to be filled in a variable named Objecttofill.

Inside the JobDone rutine I use this code:
<code>
Select case Objecttofill
Case "co_cities"
Fill_co(co_cities,list)
Case "co_customers"
Fill_co(co_customers,list)
End Select
</code>

Instead of this code, I would like to do this:
Use a rutine Fill_co2(nco as string, data as list) and call it this way:
Fill_co2(Objecttofill,list)

Inside Fill_co2 I would use a code to transform the content of the string variable nco, into a spinner variable co and then use co.Add to fill the spinner.

How can I perform the last operation
 

derez

Expert
Licensed User
Like this ? move the select to the fill sub:
B4X:
dim co as spinner
Select nco
Case "co_cities"
co  = co_cities
....

end select
co.add...
 

DanteS

Member
Licensed User
Thanks for your reply
No I don't want to write a list of Case "xxx" Fill(xxx) Case "yyy" Fill(yyy) ...
I want to write only something like Fill(nco)
 

derez

Expert
Licensed User
That is what I wrote:
B4X:
Sub Fill(nco As String, data As List)
Dim co As Spinner
Select nco
Case "co_cities"
    co  = co_cities
Case "co_customers"
    co  = co_customers
Case ...


End Select
co.add...
End Sub
Another solution:In stead of select, put the spinners in a list and get them from that list by the index number which will be supplied by nco (it will change to int):

Dim co as spinner = spinners_list.get(nco)
co.add ...

Another alternative - instead of a list, a map with the name of the spinner as key and the spinner object as value, then you get the object from the map by the name(nco) as the key.
 
Last edited:

DanteS

Member
Licensed User
Thanks again
I am going to try it using the map solution.
I asked because in .net there is something like co=FindControl(nco)
What means the "+1" answer of Erel?
 
Top