Any help here would be appreciated I have minimal experience with this language and environment.Im creating an Application which reads a csv file into a data object which is stored in an array using Kotlin in Android Studio.
The file is located in srcmainassets
please provide the solution if you know it. the record variables refer to the columns in the csv file. I have tried for 2 days to use opencvs and cant get the dependencies to work in android studio so please no opencvs!
package com.example.assignment_1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import java.io.File
class MainApplicationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_application)
val load = findViewById<Button>(R.id.loadr)
load.setOnClickListener(){
val csvFile = "C:\Users\jkmal\AndroidStudioProjects\Assignment_1\app\src\main\assets\data.csv"
var recordList = mutableListOf<Records>()
var reader = File(csvFile).readLines()
for (line in reader) {
val mbrProperties = line.split(",")
recordList.add(Records(mbrProperties[0], mbrProperties[1], mbrProperties[2], mbrProperties[3], mbrProperties[4], mbrProperties[5], mbrProperties[6], mbrProperties[7], mbrProperties[8], mbrProperties[9]))
}
}
}
}
class Records() {
var pruid: String = ""
var prname: String = ""
var prnameFR: String = ""
var date: String = ""
var numconf: String = ""
var numprob: String = ""
var numdeaths: String = ""
var numtotal: String = ""
var numtoday: String = ""
var ratetotal: String = ""
constructor(
pruid: String,
prname: String,
prnameFR: String,
date: String,
numconf: String,
numprob: String,
numdeaths: String,
numtotal: String,
numtoday: String,
ratetotal: String
) : this() {
this.pruid = pruid
this.prname = prname
this.prnameFR = prnameFR
this.date = date
this.numconf = numconf
this.numprob = numprob
this.numdeaths = numdeaths
this.numtotal = numtotal
this.numtoday = numtoday
this.ratetotal = ratetotal
}
override fun toString(): String {
return "Record [pruid=$pruid, prname=$prname, prnameFR=$prnameFR, date=$date,numconf=$numconf,numprob=$numprob,numdeaths=$numdeaths,numtotal=$numtotal,numtoday=$numtoday,ratetotal=$ratetotal ]"
}
}
SOLUTION FOUND BY ME.
package com.example.assignment_1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import java.io.BufferedReader
import java.io.InputStreamReader
class MainApplicationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_application)
var recordList = ArrayList<Records>()
val load = findViewById<Button>(R.id.loadr)
load.setOnClickListener(){
var i : Int = 0;
var fileReader = BufferedReader(InputStreamReader(resources.openRawResource(R.raw.data)))
for (line in fileReader.lines()) {
if (i == 0 )
{
}else {
val mbrProperties = line.split(",")
recordList.add(Records(mbrProperties[0], mbrProperties[1], mbrProperties[2], mbrProperties[3], mbrProperties[4], mbrProperties[5], mbrProperties[6], mbrProperties[7], mbrProperties[8], mbrProperties[9]))
}
i ++
}
for (element in recordList){
println(element)
}
}
}
}
class Records() {
var pruid: String = ""
var prname: String = ""
var prnameFR: String = ""
var date: String = ""
var numconf: String = ""
var numprob: String = ""
var numdeaths: String = ""
var numtotal: String = ""
var numtoday: String = ""
var ratetotal: String = ""
constructor(
pruid: String,
prname: String,
prnameFR: String,
date: String,
numconf: String,
numprob: String,
numdeaths: String,
numtotal: String,
numtoday: String,
ratetotal: String
) : this() {
this.pruid = pruid
this.prname = prname
this.prnameFR = prnameFR
this.date = date
this.numconf = numconf
this.numprob = numprob
this.numdeaths = numdeaths
this.numtotal = numtotal
this.numtoday = numtoday
this.ratetotal = ratetotal
}
override fun toString(): String {
return "Record [pruid=$pruid, prname=$prname, prnameFR=$prnameFR, date=$date,numconf=$numconf,numprob=$numprob,numdeaths=$numdeaths,numtotal=$numtotal,numtoday=$numtoday,ratetotal=$ratetotal ]"
}
}
question from:
https://stackoverflow.com/questions/65945761/read-into-cvs-and-save-as-a-record-object-which-is-then-loaded-into-an-array