解决:sqlite数据库含有\n的值或者sax解析xml有\n的值付给TextView,显示却不会换行
遇到个问题,网上也有人问从sqlite数据库取含有\n的值付给TextView,显示结果却不会换行,而是将\n也原原本本的显示出来了。太奇怪了!
那位老大解决过这种问题吗?给个思路...谢谢!
直接字符串中的/n或者strings.xml中的\n,textView可以自动换行,而数据库中或者xml解析后得到的字符串中的\n则原原本本的显示出来了。
自己想了个山寨办法:
List<Question> questionList = parse();
//textView.setText(questionList.get(0).getContent());
String a = questionList.get(1).getContent();
String b = "";
String aa[] = a.split("\n");
for(int i = 0;i<aa.length;i++){
if(i==0){
b = aa+"\n";
}
else if(i!=aa.length-1){
b = b + aa+"\n";
}
else{
b = b + aa;
}
}
textView.setText(b);
有更好办法的说声 thanks...
补充:
从raw/dates.txt文件取出字符串;
{"items":[{"content":"11111请选择:\nA、1成都\nB、1北京\nC、1上海\nD、1深圳","answer":"3","selectable":"1","chapterName":"1.1"},{"content":"22222请选择:\nA、2成都\nB、2北京\nC、2上海\nD、2深圳","answer":"1","selectable":"1","chapterName":"1.2"}]}
private String getDateFromTxt(){
InputStream localInputStream = getResources().openRawResource(R.raw.dates);
BufferedReader reader = null;
StringBuffer sb = null;
try {
reader = new BufferedReader(new InputStreamReader(localInputStream));
sb = new StringBuffer();
String temp = null;
while( (temp = reader.readLine())!=null) {
sb.append(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
//json解析代码
JSONArray jArr;
JSONObject j;
String content = "";
try {
j= new JSONObject(getDateFromTxt());
jArr = (JSONArray) j.get("items");
for(int i=0;i<jArr.length();i++){
JSONObject jj = (JSONObject)jArr.get(i);
content = jj.getString("content");
}
textView.setText(content);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
从文件中读出json数据.\n可以正常实现换行功能。 学习了
页:
[1]