-2

Can you help me insert the following string into TextView or ListView?

["Monday: 1:30 – 3:30 PM","Tuesday: 1:30 – 3:30 PM","Wednesday: 1:30 – 3:30 PM, 8:30 – 11:00 PM","Thursday: 1:30 – 3:30 PM, 8:30 – 11:00 PM","Friday: 1:30 – 3:30 PM, 8:30 – 11:00 PM","Saturday: 1:30 – 3:30 PM, 8:30 – 11:00 PM","Sunday: Closed"]

I need it to be shown as follows, one per line:

Monday: 1:30 – 3:30 PM
Tuesday: 1:30 – 3:30 PM
Wednesday: 1:30 – 3:30 PM, 8:30 – 11:00 PM
Thursday: 1:30 – 3:30 PM, 8:30 – 11:00 PM

Thanks!

Vladimir Jovanović
  • 2,261
  • 2
  • 24
  • 43
Rober M S
  • 13
  • 4

2 Answers2

1

If you want it to be in a single TextView is simple;

 List<String> myList = getMyListFromJSON();

 StringBuilder sb = new StringBuilder();
 for(int i = 0; i < myList.size(); i++){
      if(i+1 != myList.size()){
          sb.append(myList(i));
          sb.append("\n");
       } else { 
          sb.append(myList(i));
       }

  myTextView.setText(sb.toString());
Ricardo
  • 9,136
  • 3
  • 29
  • 35
  • thanks for answering, I have problem with getMyListFromJSON (); I do not have a list, I have a string with those values – Rober M S Feb 21 '17 at 15:26
0
String input = yourInputStringAbove;

// remove square brackets
input = input.substring(1, input.length() - 1);

// split each item by quotes and commas
String[] subset = input.split("\",\"");

// create output string (for single TextView, delete if using ListView)
String output = "";

for (String string : subset) {
  // strip quotation marks
  string = string.replace("\"", "");

  // add the string to the output
  output += string + "\n"; // (for single TextView, delete if using ListView)
}

// For a TextView
TextView textView = (TextView) findViewById(R.id.your_text_view);
textView.setText(output);

// For a ListView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.your_text_view_layout, subset);
ListView listView = (ListView) findViewById(R.id.your_list_view);
listView.setAdapter(adapter);
Charlie
  • 2,876
  • 19
  • 26