So I'm trying to read in a CSV file and show it in a ListView. I've got the csv file in the raw directory and I have successfully read each line and have it outputting to the log. The next step is to take that data and show it in a ListView. My data is stored in an ArrayList, so I'm trying to use an ArrayAdapater but I am presented with the error seen in the question title.
MainActvity.java:
private final List<Sample> coordArray = new ArrayList<>();
private void readGPSData() {
// Read the raw csv file
InputStream is = getResources().openRawResource(R.raw.rawgps);
// Reads text from character-input stream, buffering characters for efficient reading
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
// Initialization
String line = "";
// Initialization
try {
// Step over headers
reader.readLine();
// If buffer is not empty
while ((line = reader.readLine()) != null) {
Log.d("MyActivity","Line: " + line);
// use comma as separator columns of CSV
String[] tokens = line.split(",");
// Read the data
Sample sample = new Sample();
// Setters
sample.setLat(tokens[0]);
sample.setLon(tokens[1]);
// Adding object to a class
coordArray.add(sample);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, coordArray);
listView.setAdapter(adapter);
// Log the object
Log.d("My Activity", "Just created: " + sample);
}
} catch (IOException e) {
// Logs error with priority level
Log.wtf("MyActivity", "Error reading data file on line" + line, e);
// Prints throwable details
e.printStackTrace();
}
}
I've used an ArrayAdapter plenty of times and have never ran into this issue. Any help is appreciated!
List<Sample>? and same: stackoverflow.com/a/44178825/4409113