0

I'm building an Android app, where I want to read data from a CSV file and display it in some Textviews. The last column can have a larger text with line breaks in it.

Example CSV row that I'm reading to a string array :

Cat1; Cat2; Sample Text\nwith a line break

After setting the string as the text of a textview, I will get this on the device/simulator:

Sample Text\nwith a line break

If I set the string directly like this:

textView.setText("Sample Text\nwith a line break");

Or if I replace a different place holder like this:
( String in CSV: Sample Textzzzwith a line break )

textView.setText(someArray[2].replace("zzz", "\n"));

it will get me the desired result:

Sample Text
with a line break

I also tried .replace("\n", "\n") but this didn't help either.
What am I doing wrong? It's probably something basic.
I'm providing the CSV myself, so I could change something in there as well.

Thanks in advance.

Edit1: Thats how I read the CSV to a string array

int choosenfile = getResources().getIdentifier("test", "raw", getPackageName());
InputStream is = getResources().openRawResource(choosenfile);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));

String line = "";
    try{
        while ((line = reader.readLine()) != null) {
            String[] tokens = line.split(";", -1);
            someArray[0] = tokens[0];
            someArray[1] = tokens[1];
            someArray[2] = tokens[2];
        }
    } catch (IOException e1) {
        Log.e("MainActivity", "Error" + line, e1);
        e1.printStackTrace();
            }
André
  • 91
  • 1
  • 7
  • Does your CSV file contain a \ character followed by an `n` character? You might need to edit the file to replace it with a newline, and be sure to quote it properly so the newline char doesn't start a new CSV row. Or do `.replace("\\n", "\n")` to replace the pair of characters with a newline char. – Jerry101 Jan 02 '21 at 19:29
  • as in the example I have the \n between "Text" and "with". Not sure on how or where to quote in the CSV. I tried that one .replace("\\n", "\n") as well, didn't work. – André Jan 02 '21 at 19:41
  • `row that I'm reading to a string array ` You are nit showing how you read from the csv file so there is little we can do. – blackapps Jan 02 '21 at 19:59
  • `Cat1; Cat2; Sample Text\nwith a line break` That is no valid content/line for a csv file. You should at least have `Cat1;Cat2;"Sample Text\nwith a line break"`. – blackapps Jan 02 '21 at 20:01
  • @blackapps I added that part. And the quotes don't help either. It just gives me quotes on the output as well – André Jan 02 '21 at 20:08

1 Answers1

0

Given a file res/raw/data.csv with the contents:

Cat1; Cat2; Sample Text\nwith a line break

And the following Java Code

String[] someArray = new String[3];

InputStream is = getResources().openRawResource(R.raw.data);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));

String line = "";
try{
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(";", -1);
        someArray[0] = tokens[0];
        someArray[1] = tokens[1];
        someArray[2] = tokens[2];
    }
} catch (IOException e1) {
    Log.e("MainActivity", "Error" + line, e1);
    e1.printStackTrace();
}


TextView tv = findViewById(R.id.textView);
tv.setText(someArray[2].replace("\\n", "\n"));

It works as expected.

But you might want to consider the following to easily handle CSV files: https://stackoverflow.com/a/43055945/2232127


Also, your current loop will overwrite someArray on each iteration resulting in only containing the data of the last line in the file. Also make sure to close your streams when you're done using them.

JensV
  • 3,997
  • 2
  • 19
  • 43
  • I just copied the .replace part and now it works. I'm sure, I tried this several times by typing the two backslashes by hand. I don't know what went wrong there, but thanks. Don't worry about the loop, originally it is a two dimensional array. – André Jan 02 '21 at 20:44