Android Question How to reference a list indirectly

zetadan

Member
Licensed User
Longtime User
Hello,

I have searched the forum and looked at the documentation, but I cannot find what I am looking for. I need a way to reference a list when the name of the list is held in another list of strings. Hopefully, I can show what I want more precisely with some code. With this code I am trying to save the list called list1 but the name for list1 is stored in the variable listname.
B4X:
sub globals
   dim topiclist as list
   dim list1, list2 as list
   dim listname, text1, text2 as string
end sub
sub body
   text1="first"
   text2="second"
   list1.initialize
   list1.add(text1)
   list2.initialize
   list2.add(text2)
   topiclist.initialize
   topiclist.add("list1")
   topiclist.add("list2")
   listname=topiclist.get(0)
   writelist(file.dirRootExternal,listname&".txt",list1)
end sub
So in the writelist line I need to replace "list1" with the name inside the listname string. This is where I need help. I am not sure how to reference the list from a string variable.

This may be simple but I cannot figure i out.

Thanks Dan,
 

DonManfred

Expert
Licensed User
Longtime User
Hmm.. probably like this?

B4X:
Dim listmap As Map
listmap.Initialize
Dim topiclist As List
Dim list1, list2 As List
Dim listname, text1, text2 As String
text1="first"
text2="second"
list1.initialize
list1.add(text1)
listmap.Put("list1",list1)

list2.initialize
list2.add(text2)
listmap.Put("list2",list2)

topiclist.initialize
topiclist.add("list1")
topiclist.add("list2")

writelist(File.dirRootExternal,listname&".txt",listmap.Get(topiclist.get(0)))

or

B4X:
writelist(File.dirRootExternal,listname&".txt",listmap.Get("list1"))
 
Last edited:
Upvote 0

zetadan

Member
Licensed User
Longtime User
Thanks so much DonManfred.

This looks like it might be ok for my example but maybe not for what I eventually need. The name for list1 and the elements of the list will eventually be created at runtime and could be any name that the user chooses. In other words, the actual list identifiers (list1, list2, etc.) will be random and there could be any number of them. How do I avoid a statement like
B4X:
listmap.Put("list1",list1)
if I don't know the name of the list?

Dan
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The user can choose the name of a list-variable?

The user can edit a name and you will then create a new list by code, right? In code you must use a name you can identify...

B4X:
dim mynewlist as list
mynewlist.initialize
' fill the list here...
mynewlist.add(text2)
'
' Put the list in the map and take the name of it from edittext
listmap.Put(EditText1.Text,mynewlist)

Maybe i misunderstood and maybe you need to show more code or more information about your contept. What exactly you want to do?

Maybe you can make a small project which demonstrates the problem?
 
Upvote 0

zetadan

Member
Licensed User
Longtime User
It's not that the user needs to determine the name of the list variable. I will allow the user to create their own groups of words. The problem will be that I do not know exactly how many groups they will create but I want to create a list for each. So I need something like an array with a variable index. Then I need to save the list to permanent storage. I will take some time to work up a new project to explain myself better. Then see if anyone has an idea to help.

Thanks,

Dan
 
Upvote 0

zetadan

Member
Licensed User
Longtime User
Maybe I can say this better now that I realized my main problem. Before I need to save the lists, of course they need to exist and this presents the problem. I need to be able to create a number of lists where the total number of lists is user dependent. Then, I need another list (or array) to keep track of those lists. Of course the user does not need to be involved in the b4a list naming process, but I am stuck with how to create a variable number of lists. I don't want to predetermine a bunch of static list names. I guess I need an array of lists but I am not sure how to do this.

Can anyone understand what I am trying to say?
 
Upvote 0

zetadan

Member
Licensed User
Longtime User
Thanks Erel,

That is very simple and straightforward. But, what is meant by this line
B4X:
dim l1 as list = mainlist.get(0)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…