Android Question loading a csv file in a Map or HashMap

saeed10051

Active Member
Licensed User
Hi All,
i am building an application in which i have a csv file related to data of students in a class, the unique key here is the student roll number, the data include student name, class, section, grade in last year etc.
i want to load this data in my application but if i try to use a Map then there is only a key value pair and if i use a list to load the data in, then i cannot sort for a certain record based on key. can anyone help me in that. I will put my csv file in DirAssets folder so that it goes with application. Is there a way to use HashMap for this purpose.
 

José J. Aguilar

Well-Known Member
Licensed User
You can follow this sample:

Not tested, but you can use as key the roll nomber, and as value the list with data
B4X:
Dim su As StringUtils
Dim Table As List
Table = su.LoadCSV(File.DirAssets, "Trivia.csv", ",")
Dim Items() As String
Dim StudentData as List
For i = 0 To Table.Size - 1
 Items = Table.Get(i)
 StudentData.Initialize2(Items)
 Dim m As Map
 m.Initialize
 m.Put(Items(0), StudentData) '(supposing first item is roll number) 

Next
 

Erel

Administrator
Staff member
Licensed User
B4X:
Dim su As StringUtils
Dim Table As List = su.LoadCSV(File.DirAssets, "Trivia.csv", ",")
Dim m As Map
m.Initialize
For Each row() As String In Table
 Dim st As StudentType = CreateStudentType(row)
 m.Put(st.Id, st)
Next


Sub CreateStudentType (Row() As String) As StudentType
 '''
End Sub
 

William Lancee

Active Member
Licensed User
Just in case you are not used to "Type", in Process_Globals add the line below and hover/click on name of type to automatically generate the Sub

B4X:
Sub Process_Globals
    Type StudentType(Roll as String, Name as String, ...)
End Sub

'change Dim st As StudentType = CreateStudentType(row) to CreateStudentType(row(0), row(1), ...)
'or modify the arguments of the generated sub

Sub CreateStudentType (Roll as String, Name as String, ...) As StudentType
    Dim t1 as StudentType
    t1.Initialize
    t1.Roll = Roll
    t1.Name = Name
    ...
    Return t1
End Sub
 

saeed10051

Active Member
Licensed User
B4X:
Dim su As StringUtils
Dim Table As List = su.LoadCSV(File.DirAssets, "Trivia.csv", ",")
Dim m As Map
m.Initialize
For Each row() As String In Table
Dim st As StudentType = CreateStudentType(row)
m.Put(st.Id, st)
Next


Sub CreateStudentType (Row() As String) As StudentType
'''
End Sub
Thanks Erel, i need to review the documentation related to Type to fully understand it. Can you suggest any link in B4A documentation.
 

saeed10051

Active Member
Licensed User
Just in case you are not used to "Type", in Process_Globals add the line below and hover/click on name of type to automatically generate the Sub

B4X:
Sub Process_Globals
    Type StudentType(Roll as String, Name as String, ...)
End Sub

'change Dim st As StudentType = CreateStudentType(row) to CreateStudentType(row(0), row(1), ...)
'or modify the arguments of the generated sub

Sub CreateStudentType (Roll as String, Name as String, ...) As StudentType
    Dim t1 as StudentType
    t1.Initialize
    t1.Roll = Roll
    t1.Name = Name
    ...
    Return t1
End Sub
Hi William thanks for the help, i couldnt get why we are defining rows as row(o), row(1) etc. in the StudentType, i think these are various columns if we visualize it as an excel sheet and the rows will be records of various students. Also please can you guide me to some documentation that fully define this Type thing.
 
Top