I have this code:
tvData1 = (TextView)findViewById(R.id.tvData1);
tvData2 = (TextView)findViewById(R.id.tvData2);
tvData3 = (TextView)findViewById(R.id.tvData3);
GetData data= new GetData();
try {
SharedPreferences getName = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String name = getName.getString("name", "");
String returned = data.getData(name); //Gets data from internet
String[] dataSplit = returned.split(",");
for(int i = 0; i < dataSplit.length; i++){
String stringData1 = dataSplit[0];
String stringData2 = dataSplit[1];
String stringData3 = dataSplit[2];
tvData1.setText(stringData1);
tvData2.setText(stringData2);
tvData3.setText(stringData3);
}
well, the string gotten from the internet is like this:
"1,2,3 4,5,6"
and where that space is between 3 and 4 actually shows in a new line like:
1,2,3
4,5,6
but basically the result i want is
stringData1 = 1
stringData2 = 2
stringData3 = 3
stringData4 = 4
etc...
instead of:
stringData1 = 1
stringData2 = 2
stringData3 = 3 4
which is what it’s doing now.
also another irrelevant problem I seem to be having is tvData2 will not setText no matter what, even if i do tvData2.setText(“Some Text”);
String‘ssplit()method takes a regular expression. So possibly you could have a character class in the expression that would split on both commas and new-line characters. Perhaps the following would work. (Edit: Just tested it and it does work for me.)Another option could be to use the shortcut for white space, to split on all whitespace characters:
\s. (Note that you need to escape the\character:Tested and this also works, on the string
"1,2,3\n4,5,6".