Android Question how to use a single column from CSV

RSU

New Member
Licensed User
I have used us_country.csv in table view exm. but I can't use only single column instead of unusing other colm.
Secondly storeing selected Item to texbox. Please help me
 

Midimaster

Active Member
Licensed User
As I am an "old school" programmer I would say that datas never have to do anything with views. A CSV-file is a text file with lines for the rows and the cells ("columns") are strings in these lines separated by a "special character". So you have to scan the CSV by iterating through all the lines, then separating the line into single strings, then use the cell at the position x.

You can do this for each quest individually, but you can also transform the CSV into an array, where you afterwards can access each cell very fast by numbers.

Here is a example to transform a CVS into an array:

The code is not tested, only written here in the post on the fly:
B4X:
Sub CSV_Into_Array
    Dim CSV(1000,10) As String
    Dim Row As Int
    Dim Separator As String=","
    Dim Lines As List=File.ReadString("test.csv")
    For Each Line As String In Lines
        Dim LocArray() As String=Regex.Split(Separator,Line)
        For i=0 To LocArray.Length-1
            CSV(Row,i)=LocArray(i)      
        Next
        Row=Row+1
    Next
   
    ' shows all cells in column 2:
    For I=0 To Row-1
        Log( LocArray(i,2))
    Next
End Sub

Now it depends on your CSV-file, whether the code will work. You have to define the size of the array. Here in my exsapmle max. 1000 rows with max. 10 columns. And you have to find out which character is used as separator between the cells. As you did not attach your CSV-file i can only guess, that it is the "comma"-sign. But it can also be a semikolon or a TAB-Sign.
 
  • Like
Reactions: eps
Upvote 0
Top