Android,设置String Array为textview或listview
Android, set String Array to textview or listview
你能帮我把下面的字符串插入到 TextView 或 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"]
我需要显示如下,每行一个:
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
谢谢!
如果你想让它在单个 TextView 中很简单;
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());
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);
你能帮我把下面的字符串插入到 TextView 或 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"]
我需要显示如下,每行一个:
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
谢谢!
如果你想让它在单个 TextView 中很简单;
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());
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);