Android Question More than one value for a single map key ?

Devv

Active Member
Licensed User
Longtime User
hi
is it possible to have More than one value for a single map key ?

ex:
map.get1(user)
map.get2(user)
map.get3(user)

so that would get three value for a single key
is that possible , what is an alternative to the pas to so such thing ?
 

Devv

Active Member
Licensed User
Longtime User
but then how can i get all of the list second items from all the maps keys ?
in maps i used to do this

B4X:
map.Values
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Example:

B4X:
Sub Globals
   Dim m As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)  
    Dim m1 As Map
    m1.Initialize
    m1.Put("Key1", "Value1")
    m1.Put("Key2", "Value2")
    m.Initialize
    m.Put("holdkey1", m1)
End Sub

Sub Activity_Resume
    Dim m2 As Map
    m2 = m.Get("holdkey1")
    Log(m2.Get("Key2"))
End Sub
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
i have a maps file that contain's the following
ardroid=[value1, value2, value3]

B4X:
Dim mylsita As List
mylsita = shared.sources_map.GetValueAt(i)



*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 79 (Main)
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
at b4a.example.main._activity_create(main.java:491)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
** Activity (main) Resume **


any ideas why i getitng this error ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Because shared.sources_map.GetValueAt(i) returns a String and not a List.
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
How can i make it return a list ?
By putting inside the value a list instead of a string.
If you put a string in map (map.put(key,string)), map.get(key) will return a string.
If you put a list in map (map.put(key,list)), map.get(key) will return a list.
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
By putting inside the value a list instead of a string.
If you put a string in map (map.put(key,string)), map.get(key) will return a string.
If you put a list in map (map.put(key,list)), map.get(key) will return a list.


thanks for replay
i tried to do as you said but i still get the same error

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim test As Map
    Dim test2 As Map
    Dim myList As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    test.Initialize
    test2.Initialize
    myList.Initialize
   
    myList.Add("http://url.com/rss.xml")
    myList.Add("ارديويد")
    myList.Add("tech")
    test.Put("ardroid",myList)
    myList.Clear
    File.WriteMap(File.DirRootExternal,"extramap.map",test)
    Log("done")
End Sub



Sub Activity_Resume
    test2 = File.ReadMap(File.DirRootExternal,"extramap.map")
    myList = test2.Get("ardroid")
    Log(myList)
End Sub


B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
done
** Activity (main) Resume **
main_activity_resume (java line: 368)
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
    at b4a.example.main._activity_resume(main.java:368)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at b4a.example.main.afterFirstLayout(main.java:108)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

please explain what i'm doing wrong
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
File.Write map converts all values to strings. To save and retrieve objects (the list) you need to use the WriteB4xObject And ReadB4XObject methods of the RandomAccessFile library.

upload_2017-10-28_17-2-31.png
 
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Try this code:
B4X:
Sub Activity_Resume

    Private Buffer As String
	
    test2 = File.ReadMap(File.DirRootExternal, "extramap.map")  
    Buffer = test2.Get("ardroid")
    myList.AddAll(Array As String(Buffer))

    Log(myList)

End Sub

Also, remove myList.Clear from your code.
 
Last edited:
Upvote 0

Devv

Active Member
Licensed User
Longtime User
Try this code:
B4X:
Sub Activity_Resume

    Private Buffer As String
   
    test2 = File.ReadMap(File.DirRootExternal, "extramap.map") 
    Buffer = test2.Get("ardroid")
    myList.AddAll(Array As String(Buffer))

    Log(myList)

End Sub

Also, remove myList.Clear from your code.


why the list is empty ?

(ArrayList) [[]]
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
I just have the same code you have and I can see the list populated.
B4X:
** Activity (main) Create, isFirst = true **
done
** Activity (main) Resume **
(ArrayList) [http://url.com/rss.xml, ارديويد, tech, [http://url.com/rss.xml, ارديويد, tech]]
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User
I just have the same code you have and I can see the list populated.
B4X:
** Activity (main) Create, isFirst = true **
done
** Activity (main) Resume **
(ArrayList) [http://url.com/rss.xml, ارديويد, tech, [http://url.com/rss.xml, ارديويد, tech]]
plase past the project here

EDIT: sorry my fault . it is working
 
Last edited:
Upvote 0

Devv

Active Member
Licensed User
Longtime User
may anyone please tell me why this code is showing
[http://url.com/rss.xml, ارديويد, tech]
instead of "http://url.com/rss.xml"

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim test As Map
    Dim test2 As Map
    Dim myList As List
    Dim myList2 As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    test.Initialize
    test2.Initialize
    myList.Initialize
    myList2.Initialize
  
    myList.Add("http://url.com/rss.xml")
    myList.Add("ارديويد")
    myList.Add("tech")
    test.Put("ardroid",myList)
    File.WriteMap(File.DirRootExternal,"extramap.map",test)
    Log("done")
End Sub

Sub Activity_Resume

   
    test2 = File.ReadMap(File.DirRootExternal, "extramap.map")
    myList2.AddAll(Array As String( test2.Get("ardroid")))

    Log(myList2.Get(0))

End Sub
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
One way to do it:
B4X:
Sub Activity_Resume

    Private Buffer As String
    Private Items() As String
				
    test2 = File.ReadMap(File.DirRootExternal, "extramap.map")

    Buffer = test2.Get("ardroid")
    Buffer = Buffer.Replace("[", "").Replace("]", "")

    Items = Regex.Split(",", Buffer)
				
    myList = Items
				
    For I = 0 To myList.Size - 1
					
        Log("Item: " & myList.Get(I))
								
    Next
				
End Sub
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
hi
is it possible to have More than one value for a single map key ?

ex:
map.get1(user)
map.get2(user)
map.get3(user)

so that would get three value for a single key
is that possible , what is an alternative to the pas to so such thing ?

Would not it be the case to use a custom type?

B4X:
Type InfoType(device As String,string1 As String,string2 As String,string3 As String)

Dim M1 As Map :    M1.Initialize
   
Dim info As InfoType :    info.Initialize
   
info.device="android"
info.string1="value1"
info.string2="value2"
info.string3="value3"

M1.Put(user,info)

Dim getinfo As InfoType=M1.Get(user)
   
Log(getinfo.string1)

Sorry if I understood wrong.
 
Upvote 0
Top