0

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!

1

2 Answers 2

0

Problem with your code is that:

private final List<Sample> coordArray = new ArrayList<>();

Is type Sample and ArrayAdapter is type "String".

ArrayAdapter expect same type as List have it, which you put then in constructor, solution for you is having same type for example like this:

List<Sample> coordArray = new ArrayList<Sample>();
ArrayAdapter<Sample> adapter = new ArrayAdapter<Sample>(getApplicationContext(), android.R.layout.simple_list_item_1, coordArray);
Sign up to request clarification or add additional context in comments.

Comments

-1

Try using getActivity() instead of getApplicationContext().

2 Comments

I don't think this would cause issue or an error like: Cannot resolve constructor
@nordine Before you answer the question, maybe consider to test code. Just my 50cent advice to you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.