Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

android - Read into cvs and save as a Record object which is then loaded into an array

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...